jsp 没有迭代 servlet 发送的 Linkedhashmap
jsp is not iterating over Linkedhashmap sent by servlet
我正在尝试从 servlet 发送的 LinkedHashmap 生成 table 行。但是每当我执行时,我都会得到空映射或空映射。我单独尝试 运行 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"%>
我正在尝试从 servlet 发送的 LinkedHashmap 生成 table 行。但是每当我执行时,我都会得到空映射或空映射。我单独尝试 运行 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"%>