在 JSP 中遍历列表对象 [英] Iterating through a List Object in JSP

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

问题描述

我正在做一个项目来尝试自学 spring 和 struts.我目前停留在 JSP 页面上.我有一个带有变量 eid 和 ename 的 pojo 类,带有 getter/setter,我还有一个 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>

任何帮助将不胜感激!

推荐答案

在自学 Spring 和 Struts 之前,您可能应该深入了解 Java 语言.像这样输出

Before teaching yourself Spring and Struts, you should probably dive a little deeper into the Java language. Output like this

org.classes.database.Employee@d9b02

Object#toString() 方法的结果,所有对象都继承自 Object 类,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 子类通过迭代所有元素并在这些元素上调用 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 在这里

Your JSTL here

<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);
%>

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

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

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