是否可以使用jstl中的foreach同时迭代两个项目?

ran*_*ndy 14 java model-view-controller spring jsp jstl

我有两个来自我的模型的项目,我想使用jstl foreach同时迭代它们.如何使用正确的语法实现此目的?

dog*_*ane 29

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

例如,如果您有两个列表people.firstnames,则people.lastnames可以执行以下操作:

<c:forEach var="p" items="${people.firstnames}" varStatus="status">
  <tr>
      <td>${p}</td>
      <td>${people.lastnames[status.index]}</td>
  </tr>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)