is it possible to iterate two items simultaneously using foreach in jstl?

18,506

You can call varStatus.index to get the index of the current round of iteration, and then use it as a lookup for the second list.

For example, if you have two lists people.firstnames and people.lastnames you can do:

<c:forEach var="p" items="${people.firstnames}" varStatus="status">
  <tr>
      <td>${p}</td>
      <td>${people.lastnames[status.index]}</td>
  </tr>
</c:forEach>
Share:
18,506
randy
Author by

randy

Updated on June 05, 2022

Comments

  • randy
    randy almost 2 years

    I have two items from my model and I want to iterate them at the same using jstl foreach. how can I achieve this using a correct syntax?