Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over ArrayList items with JSTL forEach

Tags:

java

jsp

jstl

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.

like image 515
etaiso Avatar asked Jan 30 '26 11:01

etaiso


1 Answers

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.

like image 143
TechSpellBound Avatar answered Feb 02 '26 00:02

TechSpellBound