Question

I want to do something like this

<c:forEach var="item1" items="List1" var="item2" items="List2">
 <p> ${item1} ${item2}</p>
</c:forEach>

One solution is to iterate through both the List, if both are of same size

<c:forEach var="i" begin="0" end="$(fn:length(List1))">
 <p> <%= List1.get(i) %> <%= List2.get(i)%>  //wrong syntax 
</c:forEach>

Any idea how to achieve this.

Was it helpful?

Solution

You can call varStatus.index to get the index of the current round and then use it as a lookup for the second list. Mind the length of the Lists though or it will throw an Exception . Set the items with the List having maximum of the two.

<c:forEach var="element" items="${List1}" varStatus="status">
 <p>
  ${element}
  ${List2[status.index]}
</c:forEach>
  1. Documentation.
  2. How to avoid Java Code in JSP-Files?

OTHER TIPS

Array is Frist List, and B is Second List and varStatus.index to get the index of the current round and then use it as a lookup for the second list.
<c:forEach var="Array" items="${A}" varStatus="status">
<c:out value="${A}","${B[status.index]}"}/>
</c:forEach>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top