【问题标题】:JSTL nested forEach on Array showing up emptyJSTL 嵌套在数组上的 forEach 显示为空
【发布时间】: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 中的数据;然后检查服务器日志

标签: java jsp servlets foreach


【解决方案1】:

如果 user.firstName 出现。调试你的程序,在之后添加一个断点。添加自定义变量 allUsers.get(0) 来检查你真正得到的 allPomodoros。

【讨论】:

  • 你是对的,我的程序有问题。试图弄清楚是什么。感谢您指出。
猜你喜欢
  • 2013-01-27
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-15
  • 2019-09-24
相关资源
最近更新 更多