etaiso
etaiso

Reputation: 2746

Iterating over ArrayList items with JSTL forEach

I'm having problem running over ArrayList items and displaying them in JSP file.

Here is my bean:

public class UsersList {
    private ArrayList<UserDetails> users = new ArrayList<UserDetails>();

    public ArrayList getUsers(  ){
        return users;
    }

    public void setUsers(ArrayList<UserDetails> users){
        this.users = users;
    }
}

users contains UserDetails which is simply class with 3 String properties: email, username, password

Now I am trying to display the details in a table.. first in index.jsp I have those commands:

            UsersList bean = new UsersList();
            bean.setUsers(db.getUsersList()); // getting all user deatils from database into the list
            session.setAttribute("bean", bean);

            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/adminPage.jsp");
            dispatcher.forward(request, response);

And now on adminPage.jsp when I should display the table containing the users details, its displays nothing. Here is the code:

<c:forEach var="current" items="${bean.users}" >
<tr>
    <td>${current.email}</td>
    <td>${current.username}</td>
    <td><input type="checkbox" name="delete" value="${current.email}"</td>
</tr>
 </c:forEach>

Any idea what's the problem here?? Thanks.

Upvotes: 3

Views: 20939

Answers (2)

TechSpellBound
TechSpellBound

Reputation: 2555

Check if your prefix "c" points to the right tag library.

It should be

"http://java.sun.com/jsp/jstl/core"

If that does not work, check if there is mistake in the logic of populating the data.

Upvotes: 3

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

Since your adding the object to session you need to access it from sessionScope in EL.

<c:forEach var="current" items="${sessionScope.bean.users}" >
<tr>
    <td>${current.email}</td>
    <td>${current.username}</td>
    <td><input type="checkbox" name="delete" value="${current.email}"</td>
</tr>
 </c:forEach>

Upvotes: 0

Related Questions