简体   繁体   English

jsp不在servlet发送的Linkedhashmap上进行迭代

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

I am trying to generate table rows from LinkedHashmap sent from servlet. 我正在尝试从Servlet发送的LinkedHashmap生成表行。 But whenever i execute i get empty or null map. 但是无论何时执行,我都会得到空或空的映射。 I tried running the servlet alone to check whether the data is existing in linkedhashmap and it does. 我尝试单独运行servlet,以检查数据是否存在于linkedhashmap中并且是否存在。 But only when i pass it to the jsp page i guess i am receiving an empty map. 但是只有当我将其传递到jsp页面时,我才想我收到一个空的地图。

Below is the code 下面是代码

Jsp Code: 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 code: 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 );

}

Can anyone tell me what am i doing wrong here? 谁能告诉我我在做什么错?

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks 谢谢

LinkedHashMap is not itself an Iterable type. LinkedHashMap本身不是Iterable类型。 If you want to iterate over both the keys and values, you'd need to iterate over keys in the keyset, and fetch the corresponding values. 如果要遍历键和值,则需要遍历键集中的键,并获取相应的值。

If you're not using the type as a Map<> , I'd suggest using a list of pairs. 如果您不将类型用作Map<> ,则建议使用成对列表。

The above code is perfectly fine. 上面的代码非常好。 Just one line has to be added to the jsp file 只需将一行添加到jsp文件

<%@ page isELIgnored="false"%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM