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,040,783
  • 362
  • 3,548
  • 3,513
y07k2
  • 1,788
  • 3
  • 18
  • 35

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
  • 657,433
  • 87
  • 1,179
  • 1,226
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
  • 21,823
  • 33
  • 109
  • 156