簡體   English   中英

遍歷 JSP 中的列表 Object

[英]Iterating through a List Object in JSP

我正在做一個項目來嘗試自學 spring 和 struts。 我目前卡在 JSP 頁面上。 我有一個 pojo class 變量 eid 和 ename 以及 getter/setter,我在 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"/> 你好打印出來。 所以這讓我相信我的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>

任何幫助將不勝感激!

在自學Spring和Struts之前,你應該學習Java。 輸出像這樣

org.classes.database.Employee@d9b02

Object#toString()方法的結果,所有對象都從Object類繼承,這是Java中所有類的超類。

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的頁面,請求,會話或應用程序作用域屬性的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>

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

將代碼更改為以下內容

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

在迭代包含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>
...
<%}%>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM