Have you ever used JSTL <c:forEach> to iterate a Collection such as List or Array in JSP page? It's a basic iteration tag, accepting many different collection types and supporting subsetting and other functionality.
Step 1: Attribute list of <c:forEach>
The tag has following attributes:
Attribute | Description | Required | Default |
---|---|---|---|
items | Collection of items to iterate over. | No | None |
begin | Iteration begins at the item located at the specified index | No | 0 |
end | Iteration ends at the item located at the specified index (inclusive). If items not specified: Iteration ends when index reaches the value specified. | No | Last element |
step | teration will only process every step items of the collection, starting with the first one. | No | 1 |
var | Name of the exported scoped variable for the current item of the iteration. This scoped variable has nested visibility. Its type depends on the object of the underlying collection. | No | None |
varStatus | Name of the exported scoped variable for the status of the iteration. Object exported is of type javax.servlet.jsp.jstl.core.LoopTagStatus. This scoped variable has nested visibility. | No | None |
Step 2: Try basic loop example :
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <title>Basic <c:forEach> loop example </title> <c:forEach var="item" begin="1" end="6"> <p>Item ${item}</p> </c:forEach> </body> </html>Remember to include tag lib : http://java.sun.com/jsp/jstl/core to make this tag can execute. In will see the result of above example like followings:
Step 2: <c:forEach> Step Attribute
Change our basic example a little bit. In <c:forEach>, just add attribute step="2", and reload your page
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <title>Basic <c:forEach> loop example </title> <c:forEach var="item" begin="1" end="6" step="2"> <p>Item ${item}</p> </c:forEach> </body> </html>As you can see, the
Step 3: Iterate a Collaction (list,Array...) by using attribute: " items"
In a lot of web application, we have to print out a list of Array which is sent from Java servlet. We can use <c:for Each> to loop over all elements of list and generate HTML code to sent to display in Client.
To do in this Example, I create a ArrayList <String> in Servlet like following statements:
Listnow, I have an Array List with six Elements. In JSP page normally I have the <c:forEach> loop:codingTipList = new ArrayList (); codingTipList.add("Java"); codingTipList.add("Python"); codingTipList.add("PHP"); codingTipList.add("HTML"); codingTipList.add("CSS"); codingTipList.add("Javascript"); requestScope("codingTipList",codingTipList);
<c:forEach var="item" items="${codingTipList}"> <p>Item ${item}</p> </c:forEach>
you can add begin and end attribute, too.
<c:forEach var="item" begin="1" end="6" items="${codingTipList}" > <p>Item ${item}</p> </c:forEach>Also, do the same with step
<c:forEach var="item" begin="1" end="6" step="2" items="${codingTipList}"> <p>Item ${item}</p> </c:forEach>
and now, let's try varStatus, it's a bit different.
Step 4: <c:forEach> Show index of current Item of Collection
As I mentioned above, We have an attribute called "varStatus". It's an object save the loop Status. So, if you want to print out the index of Element when looping a list or Array, just called out index attribute of varStatus Object like following:<c:forEach var="item" begin="1" end="6" step="2" varStatus="loopStatus" items="${codingTipList}"> <p>Item ${item}: ${loopStatus.index}</p> </c:forEach>
Result: