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.
JSTL <c:forEach> Syntax Collection of items to iterate in the loop. Begin index of the iteration. Iteration begins at the value mentioned in this attribute value. (if items specified) First item has index of 0.
The <c:for each > is an iteration tag used for repeating the nested body content for fixed number of times or over the collection. These tag used as a good alternative for embedding a Java while, do-while, or for loop via a scriptlet.
varStatus is what you want! You declare a new variable within the main forEach JSP block which will allow you to access the internal loop counter of the for-each loop. Then you can call a number of methods on that object to be able to do conditional processing.
The prefix of core tag is c. Function tags. The functions tags provide support for string manipulation and string length. The URL for the functions tags is http://java.sun.com/jsp/jstl/functions and prefix is fn. Formatting tags.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With