Jumat, 30 Mei 2014

JSTL foreach tag example in JSP - looping ArrayList




JSTL  foreach loop in
JSP




JSTL  foreach tag is pretty useful
while writing Java free JSP code.  JSTL
foreach tag allows you to iterate or loop
Array List
, HashSet
or any other collection without using Java code. After introduction of JSTL and
expression language(EL) it is possible to write dynamic JSP code without using
scriptlet which clutters jsp pages. JSTL
foreach tag
is a replacement of for loop and behaves similarly like foreach
loop of Java 5 but still has some elements and attribute which makes it hard
for first-timers to grasp it. JSTL foreach loop can iterate over arrays,
collections like List,
Set
and print values just like for loop. In this JSP tutorial we will see couple of
example of foreach loop which makes it easy for new guys to understand and use
foreach loop in JSP. By the way this is our second JSP tutorial on JSTL core
library, in last tutorial we have seen How
to use core tag in JSP page
.






How to
use forEach tag in JSP page


JSTL foreach tag example in JSPforEach tag is part of standard JSTL core package and written as <foreach> or <c:foreach> or <core:foreach> whatever
prefix you are using in taglib directive while importing JSTL core library. In
order to use foreach tag in JSP pages you need to import JSTL tag library in
jsp and also need to include
jstl.jar in your WEB-INF/lib
directory
or in Java
classpath
. if you are using Eclipse
or Netbeans
IDE than it will assist you on on code completion of foreach tag otherwise you
need to remember its basic syntax as shown below:





Syntax of foreach tag in JSTL





<c:forEach var="name of
scoped variable"


           items="Colleciton,List or Array"  varStatus="status">


where var and items are
manadatory and
varStatus, begin, end or step attributes
are optional. Here is an example of foreach tag:





<c:forEach var="window" items="${pageScope.windows}">

    
<c:out value="${window}"/> 


</c:forEach>





above JSTL  foreach tag is
equivalent to following foreach loop of Java 1.5





foreach(String
window: windows){

   System.out.println(window);

}





JSTL foreach tag examples



In this section of JSTL tutorial we will see some more examples of using foreach tag in JSP
page for looping purpose. Just try couple of example and you will get hold of
foreach it looks
more easy after trying few examples.





Iterating
over collections or List using JSTL forEach loop


In order to iterate over collections e.g. List
or Set
you need to create those collections and store that into any scope
mentioned above e.g.
pageScope and than access it using
expression language like
${pageScope.myList}. see the JSP page in last example
for complete code example of foreach tag.





Iterating
over array using JSTL  forEach loop


For iterating over an array e.g. String array or integer array in JSP
page,  
"items"
attribute must resolved to an array.
You can use expression language to get an Array stored in of scope available in
JSP e.g. page scope, request scope, session or application scope. These are
different than bean
scope in Spring MVC
and don’t confuse between Spring bean scope and JSP
variable scope if you are using Spring MVC in your Java web application. Rest
of foreach loop will be similar to foreach loop example of iterating over
Collections in Java.








JSTL  foreach example using varStatus variable


varStatus attribute declare name of
variable which holds current looping counter for
foreach tag. It
also expose several useful information which you can access using
varStatus e.g. what
is current row, whether you are in last row etc. Here is a code example of how
to use
varStatus in foreach JSTL tag on JSP:





<%-- JSTL foreach tag varStatus
example to show count in JSP  --%
>

<c:forEach var="window" items="${pageScope.windows}" varStatus="loopCounter" >

    
<c:out value="count: ${loopCounter.count}"/>


 
 
<c:out value="${window}"/>

</c:forEach>



Output:

JSTL foreach tag example in JSP

count: 1 Windows XP

count: 2 Windows 7

count: 3 Windows 8

count: 4 Windows mobile








Some other handy properties are : first, last, step, begin,
end, current, index and count





Nested
foreach tag example in JSP JSTL


Another good thing of JSTL foreach tag is you can nest foreach
tag loop
inside another foreach tag which is quite powerful way of looping without using
scriptlets in JSP. Here is an example of nesting foreach tag in JSP JSTL tag
library:





<%-- JSTL foreach tag example to
loop an array in JSP and nesting of foreach loop --%
>

<c:forEach var="window" items="${pageScope.windows}" varStatus="loopCounter" >

   
<c:out value="outer loop count: ${loopCounter.count}"/> 


   <c:forEach var="window" items="${pageScope.windows}" varStatus="loopCounter" > 

       
<c:out value="inner loop count: ${loopCounter.count}"/>

  
</c:forEach>

</c:forEach>



Output:

outer loop count: 1

inner loop count: 1

inner loop count: 2

outer loop count: 2

inner loop count: 1

inner loop count: 2





Complete
JSTL  foreach loop example in JSP


Here is complete JSP page which shows how
to use JSTL foreach tag for looping over String array
. Similarly you can
loop over any Collection
e.g. List or Set as well.





<%@page import="java.util.List"%>

<%@page import="java.util.Arrays"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

Transitional//EN"

 
 "http://www.w3.org/TR/html4/loose.dtd">


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html>

   
<head>

      
<meta
http-equiv="Content-Type" content="text/html; charset=UTF-8">

      
<title>Welcome to JSTL foreach tag Example in
JSP
</title>

   
</head>



   
<body>

       
<h2>JSTL
foreach tag example in JSP
</h2>



        
<jsp:scriptlet>

            String[] windows = new
String[]{"Windows XP", "Windows 7", "Windows 8",
"Windows mobile"};

           
pageContext.setAttribute("windows", windows);

       
</jsp:scriptlet>



        
<%-- JSTL foreach
tag example to loop an array in jsp --%>


        
<c:forEach var="window" items="${pageScope.windows}"> 


            <c:out
value="${window}"/
> 


        </c:forEach>

   
</body>

</html>



Output:

JSTL foreach tag example in JSP

Windows XP

Windows 7

Windows 8

Windows mobile








That’s all on How to use JSTL forEach loop example in JSP page. We have
seen JSTL foreach tag example of
Iterating or looping over Array
, List, Collection and nesting of two
forEach loop which allows you to write powerful JSP pages without using any
Java code.





Other JSP tutorials from Javarevisited you may like






























Source:http://javarevisited.blogspot.com/2012/10/jstl-foreach-tag-example-in-jsp-looping.html

1 komentar: