JSP c:forEach 没有给出任何输出

JSP c:forEach not giving any output

我正在试验一些 JSP 和 Java 列表。

我遇到的问题是 JSP 中的 c:forEach 循环没有遍历列表。

这里是 JSP:

<%@page contentType="text/html" pageEncoding="UTF-8" import="model.UserBean" import="java.util.List" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">        
        <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/newcss.css">

        <title>Admin - Manage Users</title>
    </head>
    <body>
         <% UserBean currentUser = (UserBean) (session.getAttribute("currentSessionUser")); %>
         <% List<UserBean> uList = (List<UserBean>) (session.getAttribute("userList")); %>
        <div class="grid-container-title">
            <div class="grid-item-title"><img src="${pageContext.request.contextPath}/resources/logo.svg" width="150" height="150" class="d-inline-block align-top" alt="logo"></div>
            <div class="grid-item-title"><h1>Resource Portal - Manage Users</h1></div>
        </div>
        <h1>This is the Mage User Page</h1>
        <p> Welcome <%= currentUser.getuName()%></p>
        <div class="grid-container4">
            <div class="grid-item4l"> ID</div>
            <div class="grid-item4l"> Name </div>
            <div class="grid-item4l"> Email </div>
            <div class="grid-item4l"> Role / Status </div>
    <c:forEach items="${uList}" var="theuser"> 

            <div class="grid-item4"> ${theuser.uID} </div>
            <div class="grid-item4"> ${theuser.uName} </div>
            <div class="grid-item4"> ${theuser.uEmail} </div>
            <div class="grid-item4"> ${theuser.uRole} / ${theuser.uStatus} </div>


    </c:forEach> 
        </div>
    </body>
</html>

当我在 Debug (Netbeans) 中 运行 以上内容时,我可以看到 uList 个对象在列表中具有预期数量的 UserBean 对象。

调试还显示在 <c:foreach 行之后它完全跳过了循环。

我最初使用 getter 来获取每个列表项的值,例如:

        <div class="grid-item4"> ${theuser.getuID()} </div>

但这也没有用。

ListUsersSvlt 具有以下内容来填充列表,这是有效的(通过调试和 JUnit 测试确认):

protected void pr0ocessRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    UserDAO um = new UserDAO();

    List<UserBean> ubl = null;

    ubl = um.getAllUsers();

    response.setContentType("text/html;charset=UTF-8");
    HttpSession session = request.getSession(true);
    session.setAttribute("userList", ubl);
    response.sendRedirect("manageUsers/listUsers.jsp");
}

只需像下面这样更改 servlet 代码:

protected void pr0ocessRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    UserDAO um = new UserDAO();

    List<UserBean> ubl = null;

    ubl = um.getAllUsers();

   request.setAttribute("userList", ubl);
   request.getRequestDispatcher("manageUsers/listUsers.jsp").forward(request, response);
}

希望它能为您服务:)

将 JSP 更改为使用 Java 而不是标签有效,即更改为:

<c:forEach items="${uList}" var="theuser"> 

        <div class="grid-item4"> ${theuser.uID} </div>
        <div class="grid-item4"> ${theuser.uName} </div>
        <div class="grid-item4"> ${theuser.uEmail} </div>
        <div class="grid-item4"> ${theuser.uRole} / ${theuser.uStatus} </div>


</c:forEach> 

为此:

        <% for ( UserBean theUser : uList )
        {
            %>
        <div class="grid-item4"> <%= theUser.getuID() %> </div>
        <div class="grid-item4"> <%= theUser.getuName() %> </div>
        <div class="grid-item4"> <%= theUser.getuEmail() %> </div>
        <div class="grid-item4"> <%= theUser.getuRole() %> / <%= theUser.getuStatus() %> </div>

        <%
        }
        %>