Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop over something a specified number of times in JSTL?

Tags:

loops

jsp

jstl

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.

like image 996
Jonathan Hult Avatar asked May 23 '11 15:05

Jonathan Hult


People also ask

How do I iterate through an object in JSTL?

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.

Is for loop used in JSTL?

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.

What is varStatus in forEach of JSTL?

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.

What is C tag in JSP?

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.


1 Answers

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> 
like image 172
BalusC Avatar answered Sep 19 '22 02:09

BalusC