在JSP中遍历List对象

17

我正在做一个项目,试图学习Spring和Struts。目前在JSP页面上卡住了。我有一个包含eid和ename变量的POJO类,以及一个具有相同值的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]

我觉得可能是我在JSTL上漏了什么,但是我的 META-INF/lib 文件夹中有jstl-1.2。我还尝试将其添加到配置路径文件中,但仍然无效。我也有正确的标签URL。
当我执行简单的 <c:out value="Hello"/>时,Hello确实会打印出来。这让我相信我的 jstl 正常工作,但是当我尝试使用jstl迭代我的列表时,什么也没有显示。

无论如何,这是我的JSP页面:

<%@ 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>

任何帮助都将不胜感激!

5个回答

31

在学习Spring和Struts之前,您可能需要更深入地了解Java语言。输出内容如下:

org.classes.database.Employee@d9b02

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

List子类通过迭代所有元素并调用toString()来实现这一点。

然而,看起来您尚未在Employee类中实现(覆盖)该方法。

您的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>

除了你没有名为eList的页面,请求,会话或应用程序范围属性之外,一切都很好。

你需要添加它。

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

或者在 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>

5
将代码更改为以下内容。
<%! 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>

7
请不要将脚本标签作为EL和JSTL的替代方案。 - Sotirios Delimanolis
2
@Alaa Abuzaghleh 请阅读关于为什么不使用Scriptlets的文章 http://www.coderanch.com/how-to/java/WhyNotUseScriptlets - Prabhaker A

4
您可以直接在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>

@jjcode 不客气。sessionScope 隐含对象提供了会话作用域属性,因此不需要使用脚本 let。阅读有关 EL 隐含对象的更多信息,请访问 http://www.journaldev.com/2064/jsp-expression-language-el-example-tutorial。 - Prabhaker A

0
 <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>

-2

另一个例子只使用脚本,当迭代包含映射的ArrayList时。

<%   
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>
...
<%}%>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接