Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to loop through 2 arrays at the same time in JSTL

Tags:

jsp

jstl

I have two arrays that I need to loop through. Using foreach, I can only loop through one at a time. A regular for(i = 0; i<7; i++) Loop would be great.

like image 606
tzippy Avatar asked Jul 20 '10 12:07

tzippy


2 Answers

I think I see what you mean - you have two arrays (probably of equal size), and you want to loop so that you use the loop index to access each array.

If that's what you meant (and it's far from clear from your question), then you could do something like this (assuming arrayX and arrayY).

<c:forEach items="${arrayX}" varStatus="loop">
    <c:out value="${arrayX[loop.index]}"/>
    <c:out value="${arrayY[loop.index]}"/>
</c:forEach>

This uses arrayX to get the iterator, but then uses indexed lookups into arrayX and arrayY.

varStatus is described here .

like image 76
skaffman Avatar answered Oct 11 '22 23:10

skaffman


Here is something from JSTL in Action:

 <c:forEach begin="1" end="5" var="current">
    <c:out value="${current}"/>
</c:forEach>
like image 30
bakkal Avatar answered Oct 11 '22 21:10

bakkal