【发布时间】:2016-10-13 19:19:45
【问题描述】:
我有一个 adminpanel.jsp 页面,它应该显示所有用户的名称和他们的每个数组。我有一个嵌套的 forEach 循环。顶级循环工作正常,它按要求显示信息。
第二个循环作为一个空数组出现。 []
我在我的程序中检查过,数组本身已填充。
这是我的 .jsp 页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Admin Panel</title>
</head>
<body>
<%
UserService us = ServiceProvider.getUserService();
List<User> allUsers = us.getAllUsers();
request.setAttribute("allUsers", allUsers);
%>
<h1>This is the admin panel</h1>
<c:forEach step="1" var="user" items="${allUsers}">
<h3>${user.firstName} ${user.lastName}</h3>
<c:out value="${user.allPomodoros}" /> // shows up empty
<c:forEach step="1" var="pomodoro" items="${user.allPomodoros}">
<table>
<tr>
<th>User ID</th>
<th>Start Time</th>
<th>End Time</th>
<th>Completed</th>
<th>Pomodoro ID</th>
</tr>
<tr>
<td>${pomodoro.userID}</td>
<td>${pomodoro.beginTime}</td>
<td>${pomodoro.endTime}</td>
<td>${pomodoro.isCompleted}</td>
<td>${pomodoro.pomodoroID}</td>
</tr>
</table>
</c:forEach>
</c:forEach>
</body>
</html>
如果您需要任何其他信息,请告诉我。
【问题讨论】:
-
如果 ${user.allPomodoros} 不打印任何内容,则 getAllPomodoros() 方法返回 null 或空集合。发布的代码中没有任何内容可以让我们产生其他想法:没有用户代码,没有打印表明集合不是空的,等等。另外,删除 step="1":这是无用的噪音。
-
使用 System.out.println(...) 验证 bean 中的数据;然后检查服务器日志