JSTL XML Tag forEach

JSTL XML Tag forEach


Posted in : Java Posted on : April 26, 2012 at 8:07 PM Comments : [ 0 ]

In this tutorial you will learn about the JSTL xml tag forEach.

JSTL XML Tag forEach

In this tutorial you will learn about the JSTL xml tag forEach.

<x:forEach> tag in JSTL is used for iterating over the parsed element.

Attributes of <x:forEach> tag

  • select : This is a required attribute used for evaluating the XPath expression.
  • var : This is an optional attribute and is used for specifying the name of variable for the current item of the iteration.
  • begin : This is an optional attribute that specifies the starting item at the specified index from where the iteration will start.
  • end : This is an optional attribute that specifies the end item at the specified index (inclusive) the iteration will end.
  • step : This is an optional attribute that specifies the step item the iteration will iterate.
  • varStatus : This is an optional attribute that specifies the status of the iteration.

Example :

An example is being given below will demonstrate you about the use of JSTL xml <x:forEach> tag. In this example at first I have created an xml file named employeeDetail.xml. Then created a JSP page to parse this xml file and read data from them. On the JSP page I have used the <c:import> tag from JSTL core to import the xml file then I have used the <x:parse> from the JSTL xml to parse this xml file. After parsing the file to read data from the file I have used the <x:forEach> tag. This tag will iterate over the data and will return values from the parsed data.

employeeDetail.xml

<?xml version="1.0" ?>
<employees> 
<employee>
<name>Noor An Ahmed</name>
<age>33</age>
<dob>12-07-1979</dob>
</employee>
<employee>
<name>Ankit Sharma</name>
<age>25</age>
<dob>05-06-1987</dob>
</employee>
<employee>
<name>Rajesh Sharma</name>
<age>30</age>
<dob>06-05-1982</dob>
</employee>
<employee>
<name>Manish Pandey</name>
<age>33</age>
<dob>07-12-1979</dob>
</employee>
<employee>
<name>Jaydeep</name>
<age>32</age>
<dob>02-08-1980</dob>
</employee>
<employee>
<name>Amit Pandey</name>
<age>27</age>
<dob>06-05-1985</dob>
</employee>
</employees>

JstlXmlForEach.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSTL x:forEach Example</title>
</head>
<body>
<c:import url="employeeDetail.xml" var="e" /> 
<x:parse doc="${e}" var="empRecord" />
<p>Details of Employees</p>
<table>
<x:forEach var="emp" select="$empRecord/employees/employee">
<tr><td align="center">Name : </td> <td><x:out select="$emp/name" /></td></tr>
</x:forEach>
</table>
</body>
</html>

How to run this example

Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :

  • jstl.jar
  • standard.jar
  • xalan-2.3.1.jar
  • xercesImpl-2.7.1.jar

After adding of these jar files you may execute your program in the following ways :

  • Select JstlXmlForEach.jsp file of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • On the Eclipse Editor go to your JstlXmlForEach.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
  • A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Run On Server -> Choose your server -> Finish

NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.

Output :

When you will execute the above JSP page you will get the output on your eclipse browser as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics