In this tutorial you will learn about the JSTL xml tag when.
JSTL XML Tag When
In this tutorial you will learn about the JSTL xml tag when.
<x:when> tag in JSTL xml library is a subtag of <x:choose> which is used for executing the conditional statement. This tag returns the boolean value, if it is true then the body of this tag will be executed.
Attributes of <x:when> tag
- select : This is a required attribute which test condition specifies whether the body of this tag will be executed or not.
Example :
In the example given below you will see how the JSTL xml <x:when> tag is processed its body. In this example at first I have created an XML file. This file will be parsed on the JSP page to read the data. Now to read data from xml file I have created a JSP page where import the xml file using <c:import> tag from JSTL core and stored it into a variable named 'stu' then parsed this file by using <x:parse> from the JSTL xml and saved it into a variable named 'doc'. In the next line I have used the <x:choose>, <x:when> and <x:otherwise> tags. <x:when> tag will be executed if its select attribute returns true.
studentDetail.xml
<?xml version="1.0" ?> <students> <student> <name>Amit</name> <roll>33</roll> <grade>A</grade> </student> <student> <name>Mukesh</name> <roll>47</roll> <grade>B</grade> </student> <student> <name>Manoj</name> <roll>24</roll> <grade>B</grade> </student> <student> <name>Rajnish</name> <roll>84</roll> <grade>C</grade> </student> </students>
JstlXmlWhen.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!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:choose Example</title> </head> <body> <c:import url="studentDetail.xml" var="stu"/> <x:parse doc="${stu}" var="doc" /> <x:choose> <x:when select="$doc/students/student[grade = 'A']" > <p>You have gotten the 65% marks</p> </x:when> <x:otherwise> <p>You have gotten either below 65% or higher than 65%</p> </x:otherwise> </x:choose> </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 JstlXmlWhen.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 JstlXmlWhen.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 :
[ 0 ] Comments