一尘不染

如何在JSP页面中显示集合

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中显示

请帮忙


阅读 324

收藏
2020-06-08

共1个答案

一尘不染

您正在创建servlet的 全新 实例作为jsp:useBean。这是没有道理的。它与正在运行的servlet实例不同。

在servlet中, 甩掉

List<ContactDetails> contactsList = new ArrayList<ContactDetails>();


public List<ContactDetails> getContactsList() {
    return contactsList;
}

public void setContactsList(List<ContactDetails> contactsList) {
    this.contactsList = contactsList;
}

并更换

contactsList = assetDao.loadAllContacts();
response.setContentType("text/html");

通过

List<ContactDetails> contactsList = assetDao.loadAllContacts();
request.setAttribute("contactsList", contactsList); // This sets ${contactsList}

在JSP中, 甩掉

<jsp:useBean class="com.ey.asset.servlet.ContactManager"
scope="page"  id="contactManager">
   </jsp:useBean>

并更换

<c:forEach var="conatctInfo" items="${contactManager.contactsList}">

通过

<c:forEach var="conatctInfo" items="${contactsList}">

(并纠正错字)

2020-06-08