我的servlet類和jsp如下所示。如何在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
沒有顯示請幫助
你似乎有一些拼寫錯誤:'conatctInfo',而不是'contactInfo'。您的實際JSP是否一致? – 2011-03-09 03:58:36