JSTL foreach:获取Next对象

R4j*_*R4j 8 jsp jstl

我需要将列表中的显示产品分成3列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>
Run Code Online (Sandbox Code Playgroud)

问题是如何在列表中获得下一个产品?

Bal*_*usC 16

Skaffman给出了一个很好的答案.作为替代方案,您也可以将<tr>循环外部放在</tr><tr>正确的时刻(即每隔3个项目)打印中间体.

<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>
Run Code Online (Sandbox Code Playgroud)