簡體   English   中英

jsp不在servlet發送的Linkedhashmap上進行迭代

[英]jsp is not iterating over Linkedhashmap sent by servlet

我正在嘗試從Servlet發送的LinkedHashmap生成表行。 但是無論何時執行,我都會得到空或空的映射。 我嘗試單獨運行servlet,以檢查數據是否存在於linkedhashmap中並且是否存在。 但是只有當我將其傳遞到jsp頁面時,我才想我收到一個空的地圖。

下面是代碼

Jsp代碼:

<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <script src="http://code.jquery.com/jquery-1.10.2.js"  type="text/javascript"></script>
    <script src="js/app-ajax.js" type="text/javascript"></script>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
</head>

<body id = body>
<form action="Servlet" method="get">        
    <title>Enter Search Item</title>
    <br>
    <%
    String val = request.getParameter( "search" );
    if ( val!= null ) {
        val = val.toString() ;
   } else {
       val ="";
    }
    %>
        Search Item: <input type="text" name = "searchTerm" value = '<%=val%>' >
    <br>
    <input type="submit" value="Search" id ="button">
</form>
<div>
    <table>
            <c:if test="${not empty Documents}">
            <thead>
            <tr>
                    <td><h2>Title</h2></td>
                     <td><h2>Overview</h2></td>
                </tr>
            </thead>
            <tbody>
                <c:forEach items="${Documents}" var="document">
                    <tr>
                        <td><a href = ${document.key}>${document.value}</a></td>
                        <td></td>
                    </tr>
                </c:forEach>
            </tbody>
            </c:if>

    </table>
</div> 

</body>

Servlet代碼:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String search = request.getParameter("searchTerm");
    LinkedHashMap<String, String> Docs = null;
    ASearch ad;
    try {
        ad = new ASearch();
        Docs = ad.getData(search);
    } catch (Exception e) {
        e.printStackTrace();
    }
    response.setContentType("text/html");
    request.setAttribute("Documents", Docs); // Docs is not empty/NULL.
    RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp?search="+search);

    dispatcher.forward( request, response );

}

誰能告訴我我在做什么錯?

任何幫助,將不勝感激。

謝謝

LinkedHashMap本身不是Iterable類型。 如果要遍歷鍵和值,則需要遍歷鍵集中的鍵,並獲取相應的值。

如果您不將類型用作Map<> ,則建議使用成對列表。

上面的代碼非常好。 只需將一行添加到jsp文件

<%@ page isELIgnored="false"%>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM