【问题标题】:JSTL foreach: get Next objectJSTL foreach:获取下一个对象
【发布时间】:2011-05-04 14:03:40
【问题描述】:

我需要将列表中的产品显示为带有foreach 的 3 列。

这是我的代码:

<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>

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

【问题讨论】:

    标签: jsp jstl


    【解决方案1】:

    斯卡夫曼给出了一个很好的答案。作为替代方案,您也可以将&lt;tr&gt; 放在循环之外,并在适当的时刻(即每第三项)打印中间的&lt;/tr&gt;&lt;tr&gt;s。

    <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>
    

    【讨论】:

      猜你喜欢
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 2017-02-22
      • 2012-09-12
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      相关资源
      最近更新 更多