Shrikant Kakani
Shrikant Kakani

Reputation: 1561

Iterating through ArrayList in JSP in Spring using foreach

I have the following code for iterating through the arrayList:

    <form:forEach items="${list}" var="list">
    <tr>
    <td><c:out value="${list}" /><td>
    </tr>
</form:forEach>

Now, my code is iterating through the list but it is printing all the values at once instead of printing one by one as shown below:

[24873, 24872, 24875, 24874, 24877, 24876, 24879, 24878, 24865, 24864] 

How can I make it work to print them line by line?

Upvotes: 0

Views: 1713

Answers (1)

Prasad
Prasad

Reputation: 3795

Try this:

    <c:forEach items="${list}" var="listNumber" varStatus="listStatus">
       <c:if test="${listStatus.index < n}">
       <tr>
    <td><c:out value="${listNumber}" /><td>
       </tr>
       </c:if>
    </c:forEach>

When you specify var also as list and access it in c out: it picks up the entire list not each element in the list. To restrict display to n values, you need to specify the number in the above code.

Upvotes: 3

Related Questions