代码之家  ›  专栏  ›  技术社区  ›  Ksc

jsp没有在servlet发送的Linkedhashmap上迭代

  •  1
  • Ksc  · 技术社区  · 11 年前

    我正在尝试从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 回复  |  直到 11 年前
        1
  •  0
  •   Minty Fresh    11 年前

    LinkedHashMap 它本身不是 Iterable 类型如果您想遍历键和值,则需要遍历键集中的键,并获取相应的值。

    如果您没有将类型用作 Map<> ,我建议使用成对列表。

        2
  •  0
  •   Ksc    11 年前

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

    <%@ page isELIgnored="false"%>
    
    推荐文章