在JSP中迭代List对象 [英] Iterating through a List Object in JSP

查看:154
本文介绍了在JSP中迭代List对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

[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 文件夹。我也尝试在配置路径文件中添加它,但仍然没有。我也有正确的标签网址。
当我做一个简单的< c:out value =Hello/> 时。你好打印出来。所以这让我相信我的 jstl 工作正常,但当我尝试使用 jstl 迭代遍历我的列表时完全显示出来。

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

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:

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

推荐答案

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

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

org.classes.database.Employee@d9b02

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

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

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

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.

您的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 的作用域属性。

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

您需要添加它

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

或者在<中使用 empList 属性code> forEach 。

Or use the attribute empList in the forEach.

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

这篇关于在JSP中迭代List对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆