在JSP中迭代List对象

时间:2021-04-21 22:34:23

I am working on a project to try and teach myself spring and struts. I am currently stuck on a JSP page. I have a pojo class with variables eid and ename with getters/setters, I also have a table in sql with the same values with six populated rows.
I am accessing my database through a JdbcTemplate and have stored the result in a list, I then passed this list to my action page in which I set it as a request.setAttribute("empList",eList). In my jsp page I call that attribute and then try to iterate through it using JSTL.
However nothing shows up, I know that my list variable has data in it since i checked it using the expression tag <%=eList%> and objects show up like this:

我正在研究一个项目,试图教自己弹簧和支柱。我目前停留在JSP页面上。我有一个带变量eid的pojo类和带有getter / setter的ename,我在sql中也有一个表,其中包含六个填充行的相同值。我通过JdbcTemplate访问我的数据库并将结果存储在列表中,然后我将此列表传递给我的操作页面,我将其设置为request.setAttribute(“empList”,eList)。在我的jsp页面中,我调用该属性,然后尝试使用JSTL迭代它。但是没有任何显示,我知道我的列表变量中有数据,因为我使用表达式标签<%= eList%>检查它,对象显示如下:

[org.classes.database.Employee@d9b02, 
org.classes.database.Employee@13bce7e, 
org.classes.database.Employee@171cc79, 
org.classes.database.Employee@272a02, 
org.classes.database.Employee@137105d, 
org.classes.database.Employee@1359ad]

I thought that maybe I was missing something on jstl but I have jstl-1.2 in my META-INF/lib folder. I have also tried to add it in the configure path file and still nothing. I also have the correct tag url.
Also when I do a simple <c:out value="Hello"/>. Hello does print out. So this leads me to believe that my jstl is working properly, but when I try iterating through my list using jstl nothing shows up at all.

Anyways here is my JSP page:

我想也许我在jstl上遗漏了一些东西,但我的META-INF / lib文件夹中有jstl-1.2。我也尝试在配置路径文件中添加它,但仍然没有。我也有正确的标签网址。当我做一个简单的 时。你好打印出来。所以这让我相信我的jstl工作正常,但是当我尝试使用jstl遍历我的列表时,根本没有任何显示。无论如何这里是我的JSP页面: :out>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-   8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.List"%>
<!DOCTYPE html>
<% List eList = (List)session.getAttribute("empList");%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Employee Details</title>
</head>
<body>
<c:out value="Hello"></c:out>
<h3>Employee Details</h3>
<hr size="4" color="gray"/>
<table>
<%=eList%>
    <c:forEach items="${eList}" var="employee">
        <tr>
            <td>Employee ID: <c:out value="${employee.eid}"/></td>
            <td>Employee Pass: <c:out value="${employee.ename}"/></td>  
        </tr>
    </c:forEach>
</table>
</body>
</html>

Any help would be highly appreciated!

任何帮助将非常感谢!

5 个解决方案

#1


24  

Before teaching yourself Spring and Struts, you should probably learn Java. Output like this

在自学Spring和Struts之前,你应该学习Java。像这样输出

org.classes.database.Employee@d9b02

is the result of the Object#toString() method which all objects inherit from the Object class, the superclass of all classes in Java.

是Object#toString()方法的结果,所有对象都从Object类继承,这是Java中所有类的超类。

The List sub classes implement this by iterating over all the elements and calling toString() on those. It seems, however, that you haven't implemented (overriden) the method in your Employee class.

List子类通过迭代所有元素并在那些元素上调用toString()来实现它。但是,您似乎没有在Employee类中实现(覆盖)该方法。

Your JSTL here

你的JSTL在这里

<c:forEach items="${eList}" var="employee">
    <tr>
        <td>Employee ID: <c:out value="${employee.eid}"/></td>
        <td>Employee Pass: <c:out value="${employee.ename}"/></td>  
    </tr>
</c:forEach>

is fine except for the fact that you don't have a page, request, session, or application scoped attribute named eList.

除了您没有名为eList的页面,请求,会话或应用程序范围属性之外,这样做很好。

You need to add it

你需要添加它

<% List eList = (List)session.getAttribute("empList");
   request.setAttribute("eList", eList);
%>

Or use the attribute empList in the forEach.

或者在forEach中使用empList属性。

<c:forEach items="${empList}" var="employee">
    <tr>
        <td>Employee ID: <c:out value="${employee.eid}"/></td>
        <td>Employee Pass: <c:out value="${employee.ename}"/></td>  
    </tr>
</c:forEach>

#2


3  

change the code to the following

将代码更改为以下内容

<%! List eList = (ArrayList)session.getAttribute("empList");%>
....
<table>
    <%
    for(int i=0; i<eList.length;i++){%>
        <tr>
            <td><%= ((Employee)eList[i]).getEid() %></td>
            <td><%= ((Employee)eList[i]).getEname() %></td>  
        </tr>
      <%}%>
</table>

#3


2  

you can read empList directly in forEach tag.Try this

你可以直接在forEach标签中读取empList。试试这个

 <table>
       <c:forEach items="${sessionScope.empList}" var="employee">
            <tr>
                <td>Employee ID: <c:out value="${employee.eid}"/></td>
                <td>Employee Pass: <c:out value="${employee.ename}"/></td>  
            </tr>
        </c:forEach>
    </table>

#4


-1  

another example with just scriplets, when iterating through an ArrayList that contains Maps.

在迭代包含Maps的ArrayList时,只有scriplets的另一个例子。

<%   
java.util.List<java.util.Map<String,String>> employees=(java.util.List<java.util.Map<String, String>>)request.getAttribute("employees");    

for (java.util.Map employee: employees) {
%>
<tr>
<td><input value="<%=employee.get("fullName") %>"/></td>    
</tr>
...
<%}%>

#5


-1  

 <c:forEach items="${sessionScope.empL}" var="emp">
            <tr>
                <td>Employee ID: <c:out value="${emp.eid}"/></td>
                <td>Employee Pass: <c:out value="${emp.ename}"/></td>  
            </tr>
        </c:forEach>

#1


24  

Before teaching yourself Spring and Struts, you should probably learn Java. Output like this

在自学Spring和Struts之前,你应该学习Java。像这样输出

org.classes.database.Employee@d9b02

is the result of the Object#toString() method which all objects inherit from the Object class, the superclass of all classes in Java.

是Object#toString()方法的结果,所有对象都从Object类继承,这是Java中所有类的超类。

The List sub classes implement this by iterating over all the elements and calling toString() on those. It seems, however, that you haven't implemented (overriden) the method in your Employee class.

List子类通过迭代所有元素并在那些元素上调用toString()来实现它。但是,您似乎没有在Employee类中实现(覆盖)该方法。

Your JSTL here

你的JSTL在这里

<c:forEach items="${eList}" var="employee">
    <tr>
        <td>Employee ID: <c:out value="${employee.eid}"/></td>
        <td>Employee Pass: <c:out value="${employee.ename}"/></td>  
    </tr>
</c:forEach>

is fine except for the fact that you don't have a page, request, session, or application scoped attribute named eList.

除了您没有名为eList的页面,请求,会话或应用程序范围属性之外,这样做很好。

You need to add it

你需要添加它

<% List eList = (List)session.getAttribute("empList");
   request.setAttribute("eList", eList);
%>

Or use the attribute empList in the forEach.

或者在forEach中使用empList属性。

<c:forEach items="${empList}" var="employee">
    <tr>
        <td>Employee ID: <c:out value="${employee.eid}"/></td>
        <td>Employee Pass: <c:out value="${employee.ename}"/></td>  
    </tr>
</c:forEach>

#2


3  

change the code to the following

将代码更改为以下内容

<%! List eList = (ArrayList)session.getAttribute("empList");%>
....
<table>
    <%
    for(int i=0; i<eList.length;i++){%>
        <tr>
            <td><%= ((Employee)eList[i]).getEid() %></td>
            <td><%= ((Employee)eList[i]).getEname() %></td>  
        </tr>
      <%}%>
</table>

#3


2  

you can read empList directly in forEach tag.Try this

你可以直接在forEach标签中读取empList。试试这个

 <table>
       <c:forEach items="${sessionScope.empList}" var="employee">
            <tr>
                <td>Employee ID: <c:out value="${employee.eid}"/></td>
                <td>Employee Pass: <c:out value="${employee.ename}"/></td>  
            </tr>
        </c:forEach>
    </table>

#4


-1  

another example with just scriplets, when iterating through an ArrayList that contains Maps.

在迭代包含Maps的ArrayList时,只有scriplets的另一个例子。

<%   
java.util.List<java.util.Map<String,String>> employees=(java.util.List<java.util.Map<String, String>>)request.getAttribute("employees");    

for (java.util.Map employee: employees) {
%>
<tr>
<td><input value="<%=employee.get("fullName") %>"/></td>    
</tr>
...
<%}%>

#5


-1  

 <c:forEach items="${sessionScope.empL}" var="emp">
            <tr>
                <td>Employee ID: <c:out value="${emp.eid}"/></td>
                <td>Employee Pass: <c:out value="${emp.ename}"/></td>  
            </tr>
        </c:forEach>