jsp没有迭代servlet发送的Linkedhashmap

时间:2015-03-11 17:42:12

标签: java jsp servlets jstl

我正在尝试从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 );

}

谁能告诉我这里我做错了什么?

任何帮助都将不胜感激。

由于

2 个答案:

答案 0 :(得分:0)

LinkedHashMap本身不是Iterable类型。如果要迭代键和值,则需要迭代键集中的键,并获取相应的值。

如果您未将该类型用作Map<>,我建议您使用对名单。

答案 1 :(得分:0)

上面的代码非常好。只需要将一行添加到jsp文件

<%@ page isELIgnored="false"%>