0

So I am getting an ArrayList of StudentBeans from the session, and I want to iterate through each StudentBean and get the values, but for some reason, the elements of ArrayList are not being stored in the variable as I am getting only one 0 arg constructor StudentBean. Can you tell me where is my mistake?

    <jsp:useBean
        id="list"
        class="java.util.ArrayList"
        type="java.util.ArrayList"
        scope="session">
    </jsp:useBean>

    <jsp:useBean
        id="student"
        class="com.foo.bar.beans.StudentBean"
        type="com.foo.bar.beans.StudentBean"
        scope="page">
    </jsp:useBean>

    <table>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>ID</th>
            <th>Grade</th>
        </tr>
        <c:forEach items="${list}" var="student"> 

            <tr>
                <td>
                    x
                </td>
                <td>
                    ${student.name}
                </td>
                <td>
                    ${student.id}
                </td>
                <td>
                    ${student.grade}
                </td>
            </tr>

        </table>

    </c:forEach>

2 Answers2

3

Sorry, I am not able to do comment.

Have you included the core tag library in your JSP File?

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Maybe because of this your variable is not storing.

Ravat Tailor
  • 1,193
  • 3
  • 20
  • 44
0

As you have stated I have a servlet to make ArrayList and store it in the session lets assume that the session attribute is called sList

Then in your JSP all you need is

<c:forEach items='${sList}' var='student'>
        <tr>
            <td>
                x
            </td>
            <td>
                ${student.name}
            </td>
            <td>
                ${student.id}
            </td>
            <td>
                ${student.grade}
            </td>
        </tr>
</c:forEach> 
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64