JSP c:forEach不提供任何输出

时间:2019-02-11 19:57:39

标签: jsp javabeans jsp-tags

我正在尝试一些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对象。

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

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

        <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");
}

2 个答案:

答案 0 :(得分:1)

只需更改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);
}

希望它会达到您的目的:)

答案 1 :(得分:0)

更改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>

        <%
        }
        %>