JSTL XML flow control tags
August 23, 2010 22:55:58 Last update: August 24, 2010 15:45:04
The tags
XML flow control tags are exactly the same as their Core flow control equivalents, except that the
In a test condition, the XPath expression is evaluated to a boolean value by the rules of the XPath
Display "UK based customer" if the customer's
Display hello message with name if the customer's
Write out the name of each author for a list of authors matched by the XPath:
Test it
Make these additions to the expanded test application:
XML flow control tags are exactly the same as their Core flow control equivalents, except that the
test condition with a boolean EL expression is replaced by the select condition with an XPath expression. In the case of the forEach tag, the items attribute is replaced with the select attribute.
In a test condition, the XPath expression is evaluated to a boolean value by the rules of the XPath
boolean() function, which converts its argument to a boolean as follows:
- a number is true if and only if it is neither positive or negative zero nor NaN.
- a node-set is true if and only if it is non-empty.
- a string is true if and only if its length is non-zero.
- an object of a type other than the four basic types is converted to a boolean in a way that is dependent on that type.
<x:if>
Display "UK based customer" if the customer's
location attribute equals "UK":
<x:if select="$customer/self::node()[location='UK']"> UK based customer </x:if>
<x:choose>,<x:when>,<x:otherwise>
Display hello message with name if the customer's
firstName is not empty, display generic message otherwise:
<x:choose> <x:when select="$customer/firstName"> Hello <x:out select="$customer/firstName"/> </x:when> <x:otherwise> Hello my friend </x:otherwise> </x:choose>
<x:forEach>
Write out the name of each author for a list of authors matched by the XPath:
<x:forEach select="$doc//author"> <x:out select="@name"/><br> </x:forEach>
Test it
Make these additions to the expanded test application:
- Create a new Java class
XMLFlowControl:package jstl.demo.handler; import java.io.IOException; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.jstl.core.Config; import jstl.demo.DemoHandler; public class XMLFlowControl implements DemoHandler { public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { RequestDispatcher d = req.getRequestDispatcher("/xmlflowcontrol.jsp"); d.forward(req, resp); } }
- Create a new JSP (
xmlflowcontrol.jsp) underwebapp:<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%> <html> <head> <title>JSTL XML Flow Control Tags</title> </head> <body> <x:parse var="customers"> <customers> <customer> <location>US</location> <firstName>Daniel</firstName> <lastName>Smith</lastName> </customer> <customer> <location>UK</location> <firstName>Oliver</firstName> <lastName>Jones</lastName> </customer> </customers> </x:parse> <x:set var="customer" select="$customers//customer[2]"/> <p> <b>Testing <x:if>:</b><br> <x:if select="$customer/self::node()[location='UK']"> <x:out select='$customer/firstName'/> is located in UK. </x:if> </p> <p> <b>Testing <x:choose>, <x:when>, <x:otherwise>:</b><br> <x:choose> <x:when select='$customer/firstName'> Hello <x:out select='$customer/firstName'/>! </x:when> <x:otherwise> Hello my friend! </x:otherwise> </x:choose> </p> <p> <b>Testing <x:forEach>:</b><br> <ul> <x:forEach select="$customers//customer"> <li><x:out select="firstName"/> <x:out select="lastName"/></li> </x:forEach> </ul> </p> </body> </html>
- Compile and package the WAR with:
mvn package
- Deploy the WAR to a Tomcat or JBoss server of your choice (for example, JBoss 5.1.0 GA).
- Test the page with this URL (JBoss 5.1.0 GA or Tomcat running on port 8080):
http://localhost:8080/jstl-demo/demo/XMLFlowControl
You may adjust the URL if your servlet container runs on a different port or the web app is bound to a different context root.