Reputation: 4004
I am experimenting with some JSP and Java Lists.
The problem I have is that the c:forEach
loop in the JSP is not itertaing over the list.
Here is the 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>
When I run the above in Debug (Netbeans) I can see that uList
objects has the expected number of UserBean objects in the list.
Debug also shows that after the <c:foreach
line it skips the loop entirely.
I initially used the getters to grab the values of each list item, e.g.:
<div class="grid-item4"> ${theuser.getuID()} </div>
But that didn't work either.
The ListUsersSvlt has the following to populated the List, which is wokring (confirmed by debug and also JUnit test):
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");
}
Upvotes: 0
Views: 1212
Reputation: 4004
Changing the JSP to use Java rather than the Tags worked, i.e. changed this:
<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>
To this:
<% 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>
<%
}
%>
Upvotes: 0
Reputation: 2823
just change the servlet code like below:
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);
}
hope it will serve your purpose :)
Upvotes: 1