JSTL foreach: get Next object
I need display product in list into 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 do I get next product in list?
Skaffman has given a good answer. As an alternative, you can also just put the <tr>
outside the loop and print the intermediate </tr><tr>
s at the right moments (i.e. every 3rd item).
<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>
精彩评论