如何在jsp页面中显示集合

发布于 2024-10-20 17:26:07 字数 2545 浏览 2 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绾颜 2024-10-27 17:26:07

您正在创建一个全新 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}">

(并修正拼写错误)

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

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


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

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

and replace

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

by

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

In JSP, get rid of

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

and replace

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

by

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

(and fix the typo)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文