59

I need a while loop within JSTL. I cannot seem to find how to loop over something a specified number of times. Any ideas how I can accomplish this?

I am thinking I could use a forEach but I do not really care to loop over a collection.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jonathan Hult
  • 1,351
  • 2
  • 13
  • 19

1 Answers1

123

The <c:forEach> tag is definitely suitable for this. It has begin and end attributes where you can specify the, well, begin and end. It has a varStatus attribute which puts a LoopTagStatus object in the loop tag scope which in turn has several methods like getIndex() and on.

Here's a kickoff example:

<c:forEach begin="0" end="10" varStatus="loop">
    Index: ${loop.index}<br/>
</c:forEach>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    @jon20usa Check out the [JSTL Tag Reference](http://download.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/) to learn the finer details about these tags. – elekwent May 23 '11 at 15:14
  • @elekwent: The blueish parts in my answer are links already :) – BalusC May 23 '11 at 15:14
  • Ahh, so they are. Oops, I meant to add that comment to the original question. – elekwent May 23 '11 at 15:16
  • @elekwent: ah OK, no problem. You can also find more info and links in our [JSTL tag wiki page](http://stackoverflow.com/tags/jstl/info). Hover the `[jstl]` tag below the question until a popbox shows and click the *info* link there. – BalusC May 23 '11 at 15:17