【问题标题】:is it possible to iterate two items simultaneously using foreach in jstl?是否可以在 jstl 中使用 foreach 同时迭代两个项目?
【发布时间】:2011-05-07 18:10:26
【问题描述】:

我的模型中有两个项目,我想使用 jstl foreach 同时迭代它们。如何使用正确的语法来实现这一点?

【问题讨论】:

    标签: java model-view-controller spring jsp jstl


    【解决方案1】:

    您可以调用varStatus.index获取本轮迭代的索引,然后将其用作第二个列表的查找。

    例如,如果您有两个列表 people.firstnamespeople.lastnames,您可以这样做:

    <c:forEach var="p" items="${people.firstnames}" varStatus="status">
      <tr>
          <td>${p}</td>
          <td>${people.lastnames[status.index]}</td>
      </tr>
    </c:forEach>
    

    【讨论】:

    • 相当直接和聪明的方法。 +1
    • 很好,工作正常,但我有 3 个列表,如何迭代第三个列表?
    • @GaneshPutta,因为你已经有了索引,你可以在 n 个列表上应用相同的逻辑。 x[status.index]y[status.index]z[status.index] 等等。
    猜你喜欢
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 2020-12-27
    相关资源
    最近更新 更多