JSTL foreach: get the next object

I need to display the product in a list of 3 columns with foreach .

Here is my code:

 <table> <c:forEach items="${lstProduct}" var="product" varStatus="status" step="3"> <tr> <td> <!--product of column left will be display here --> ${product.id} ${product.name} </td> <td> <!--product of column middle will be display here --> <!--I need something like this: productMiddle = product.getNext() --> </td> <td> <!--product of column right will be display here --> <!-- productRight = productMiddle.getNext() --> </td> </tr> </c:forEach> </table> 

The question is how to get the next product on the list?

+8
jsp jstl
source share
1 answer

Scuffman gave a good answer. Alternatively, you can also just put <tr> outside the loop and print an intermediate </tr><tr> at the right times (i.e., every third element).

 <table> <tr> <c:forEach items="${lstProduct}" var="product" varStatus="loop"> <c:if test="${not loop.first and loop.index % 3 == 0}"> </tr><tr> </c:if> <td> ${product.id} ${product.name} </td> </c:forEach> </tr> </table> 
+16
source share

All Articles