如何在jsp页面中显示集合
我的 servlet 类和 jsp 如下所示。
ContactManager.java
public class ContactManager extends HttpServlet {
List<ContactDetails> contactsList = new ArrayList<ContactDetails>();
public List<ContactDetails> getContactsList() {
return contactsList;
}
public void setContactsList(List<ContactDetails> contactsList) {
this.contactsList = contactsList;
}
/**
*
*/
private static final long serialVersionUID = 7999998491377176969L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
AssetDao assetDao = new AssetDao();
contactsList = assetDao.loadAllContacts();
response.setContentType("text/html");
String nextJSP = "/searchResults.jsp";
RequestDispatcher dispatcher = getServletContext()
.getRequestDispatcher(nextJSP);
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
ContactDetails.java
public class ContactDetails {
private String contactName;
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
}
searchResults.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<jsp:useBean class="com.ey.asset.servlet.ContactManager"
scope="page" id="contactManager">
</jsp:useBean>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Contacts manager</title>
</head>
<body>
Contacts List
<br><br>
<table width="40%" cellpadding="5" bordercolor="#000066"
bgcolor="#FFFFFF" border="1" cellspacing="0">
<tr>
<c:forEach var="conatctInfo" items="${contactManager.contactsList}">
<td><div align="center"><b> <c:out value="${conatctInfo.contactName}"/>
</b></div></td>
</c:forEach>
</tr>
</table>
</body>
</html>
列表大小为 2..但它没有显示在 serachResult.jsp
请帮助
My servlet class and jsp is given below.
ContactManager.java
public class ContactManager extends HttpServlet {
List<ContactDetails> contactsList = new ArrayList<ContactDetails>();
public List<ContactDetails> getContactsList() {
return contactsList;
}
public void setContactsList(List<ContactDetails> contactsList) {
this.contactsList = contactsList;
}
/**
*
*/
private static final long serialVersionUID = 7999998491377176969L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
AssetDao assetDao = new AssetDao();
contactsList = assetDao.loadAllContacts();
response.setContentType("text/html");
String nextJSP = "/searchResults.jsp";
RequestDispatcher dispatcher = getServletContext()
.getRequestDispatcher(nextJSP);
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
ContactDetails.java
public class ContactDetails {
private String contactName;
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
}
searchResults.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<jsp:useBean class="com.ey.asset.servlet.ContactManager"
scope="page" id="contactManager">
</jsp:useBean>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Contacts manager</title>
</head>
<body>
Contacts List
<br><br>
<table width="40%" cellpadding="5" bordercolor="#000066"
bgcolor="#FFFFFF" border="1" cellspacing="0">
<tr>
<c:forEach var="conatctInfo" items="${contactManager.contactsList}">
<td><div align="center"><b> <c:out value="${conatctInfo.contactName}"/>
</b></div></td>
</c:forEach>
</tr>
</table>
</body>
</html>
the list size is 2.. but its not showing in the serachResult.jsp
Please Help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在创建一个全新 servlet 实例作为
jsp:useBean
。这毫无意义。它与正在运行的 servlet 实例不是同一个实例。在 servlet 中,删除
并替换
为
在 JSP 中,删除
并替换
为
(并修正拼写错误)
You're creating a brand new instance of the servlet as a
jsp:useBean
. This makes no sense. It is not the same instance as the servlet instance which is been running.In servlet, get rid of
and replace
by
In JSP, get rid of
and replace
by
(and fix the typo)