本文介绍了JSTL foreach:获取下一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在列表中用foreach
将产品展示到3列中.
I need display product in list into 3 columns with foreach
.
这是我的代码:
<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给出了很好的答案.另外,您也可以将<tr>
放在循环之外,并在适当的时候(即每3个项目)打印中间的</tr><tr>
.
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>
这篇关于JSTL foreach:获取下一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!