Java 在 JSP 中遍历列表对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20789207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:20:41  来源:igfitidea点击:

Iterating through a List Object in JSP

javasqlspringjspjstl

提问by jjcode

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 JdbcTemplateand 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:

我正在做一个项目来尝试自学 spring 和 struts。我目前被困在 JSP 页面上。我有一个带有变量 eid 和 ename 的 pojo 类,带有 getter/setter,我在 sql 中还有一个表,它的值与六个填充行相同。
我正在通过 a 访问我的数据库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/libfolder. 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 jstlis working properly, but when I try iterating through my list using jstlnothing shows up at all.

Anyways here is my JSP page:

我想也许我在 jstl 上遗漏了一些东西,但我的META-INF/lib文件夹中有 jstl-1.2 。我也尝试将它添加到配置路径文件中,但仍然没有。我也有正确的标签网址。
当我做一个简单的<c:out value="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>

Any help would be highly appreciated!

任何帮助将不胜感激!

采纳答案by Sotirios Delimanolis

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 Objectclass, the superclass of all classes in Java.

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

The Listsub 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 Employeeclass.

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 empListin the forEach.

或使用属性empListforEach

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

回答by Prabhaker A

you can read empListdirectly in forEachtag.Try this

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

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

回答by Alaa Abuzaghleh

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>

回答by Striker

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

另一个只有脚本的示例,当迭代包含 Maps 的 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>
...
<%}%>

回答by Savan Makone

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