JSTL XML Tag if

JSTL XML Tag if


Posted in : Java Posted on : April 30, 2012 at 6:50 PM Comments : [ 0 ]

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

JSTL XML Tag if

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

<x:if> tag from JSTL xml library is used for executing the conditional statements. Body of this tag is executed when an XPath expression of the select attribute is evaluated and returns 'true'.

Attributes of <x:if> tag

  • select : This is a required attribute specifies the test condition that evaluates the XPath expression and decides whether its body should be processed or not
  • var : This is an optional attribute which stores the result of the test condition evaluation.
  • scope : This is an optional attribute specifies the scope of var attribute.

Example :

An example is being given for you which will demonstrate you about how to use the JSTL xml <x:if> tag in JSP. In this example I have created a xml file named stuReportCard.xml. Then created a JSP file where I have imported this file using <c:import> tag from JSTL core and stored it into the variable 'stu' then parsed this xml file using <x:parse> tag from the JSTL xml and stores it into a variable named 'doc' and used the <x:if> tag to test the condition.

stuReportCard.xml

<?xml version="1.0" ?>
<students> 
<student>
<name>Amit</name>
<roll>33</roll>
<grade>A</grade>
</student>
<student>
<name>Manoj</name>
<roll>24</roll>
<grade>B</grade>
</student>
<student>
<name>Rajnish</name>
<roll>84</roll>
<grade>C</grade>
</student>
<student>
<name>Ayush</name>
<roll>35</roll>
<grade>D</grade>
</student>
<student>
<name>Arpit</name>
<roll>26</roll>
<grade>E</grade>
</student>
</students>

JstlXmlIf.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:if Example</title>
</head>
<body>
<c:import url="stuReportCard.xml" var="stu"/>
<x:parse doc="${stu}" var="doc" />
<x:if select="$doc/students/student[grade = 'A']" >
<p>Grade A = 'Pass'</p>
</x:if>
<x:if select="$doc/students/student[grade = 'E']" >
<p>Grade E = 'Fail'</p>
</x:if>
</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 JstlXmlIf.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 JstlXmlIf.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