0

I'm in a forEach and would like to get an element (from listName) with proportional id as forEach loop element.

<% i=0; %>
<c:forEach var="account" items="${ctx.model.accounts}">
    <c:out value="${listName.get(${i})}" />
    <% i++; %>
</c:forEach>

Is this even possible?

I mean ${listName.get(${i})} is of course wrong, but how can I get it.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
y07k2
  • 1,898
  • 4
  • 20
  • 36

2 Answers2

3

You don't need scriptlets to achieve that.

<c:forEach var="account" items="${ctx.model.accounts}" varStatus="loopStatus">
    <c:out value="${listName[loopStatus.index]}" />
</c:forEach>

But the fact that you have two parallel lists is, IMHO, a smell. Why don't you have a single list, where each element would allow access to the account and to the corresponding element in listname?

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

${listName[i]} will give you the element from listName.

Check Get specific element in a list or array using EL for details.

Community
  • 1
  • 1
Ajinkya
  • 22,324
  • 33
  • 110
  • 161