Combine the power
of XPath and JSP
tag libraries -
JavaWorld January
2001
Tutorial Details:
Combine the power of XPath and JSP tag libraries
Combine the power of XPath and JSP tag libraries
By: By Stanley Santiago
Achieve simple data access in JSPs using XPath expressions
ava Server Pages and XML represent natural partners for building Web applications that use heterogeneous data sources. XML's DOM API provides a universal way to represent these diverse data sources. XPath provides a standard and simple syntax to access the DOM. JSP pages execute as Java servlets, typically in the context of a Web server where they generate session and data-dependent response documents. By combining these technologies, along with an XPath custom tag library, it's possible to make a standard and uniform approach to both representing the model data and accessing it.
The XPath tag library provides a framework within which dynamic content represented as a DOM document can be manipulated and inserted into a JSP to:
Simplify presentation code in the JSP and make it Java-free
Simplify templating or substitution points within the JSP
Remove complexity from dynamic data access, yet make it powerful
The XPath tag library combined with JSP further aids efficiency by engendering a clear separation between the role of a page author and a programmer.
This article covers intermediate-to-advanced JSP, XML, and DOM topics, so you may need to do some background reading in Resources to get up to speed.
Note: The XPath custom tag library is still in the proof-of-concept stage, hence it is not available as a standalone tag library. However, you can download the XPath-JSP Test Application WAR file and extract the relevant parts to create your own standalone tag library.
XPath syntax
XPath, a W3C recommendation since November 1999, provides an easy notation for specifying and selecting parts of an XML document. An XML document is a tree of elements with only one route or path from the root node of the tree to any other node of the tree. XPath defines this path.
Let's take an example XML document and some XPath expressions used to locate its parts.
The document below represents a user who has a userid and a password. Moreover, a user can have multiple roles, in this case two: Domain Administrator and Help Desk Administrator :
someone
somewhere
Domain Administrator
Help Desk Administrator
The basic XPath syntax is similar to filesystem addressing. As such, if the path starts with / , then it represents an absolute path to the required element:
/user/userid/text() select 'someone'
/user/roles/role select all 'role' elements
//role select all 'role' elements [ same as 2.]
//role[@id='admin']/text() select 'Domain Administrator'
/user/roles/role[1] select the first role element
i.e. 'Domain Administrator'
/user/roles/role[last()] select the last role element
i.e. 'Help Desk Administrator'
The above examples represent just a sample of XPath's power. For a more complete list of XPath syntax see the XPath tutorial in Resources .
Architecture for a Web application using the XPath tag library
Having seen how XPath expressions can be used on an XML document, let us now discuss the architecture of a Web application using the XPath custom tag library, illustrated in Figure 1.
Figure 1. Web application architecture using the XPath custom tag library
Click on thumbnail to view full-size image.
In this approach, presentation JSPs access the domain data or enterprise resources using XPath expressions. The domain data is represented as DOM document instances.
Page authors use the XPath custom tag library to embed XPath expressions in JSPs. Developers own the DOM document instances and are responsible for maintaining their state. Additionally, page authors could be given a DTD for the DOM document, providing its structure, which would be helpful in writing the XPath expressions.
Remember that XPath expressions are for access only, they cannot be used to update the state of a DOM document instance.
Examples of XPath expression in JSPs
We will create a sample to illustrate the various XPath custom tag library options. For this example, we assume that the application deals with customer orders. A sample Order XML document looks like this:
Order.xml
ALICE SMITH
123 MAPLE STREET
MILL VALLEY
CA
90952
12-31-2000
-
Twelve Songs of Christmas
JIM REEVES
15.95
-
First Piano Concerto
>Janos
12.95
...
...
...
The Order XML document consists of a ShipTo , Date , and repeating Item elements. The application keeps an instance of the Order XML document in memory as a DOM document object. The examples below illustrate the usage of XPath expressions in a JSP, which accesses the sample Order DOM document.
Example 1: Simple data access and substitution
In our first example, the JSP uses XPath expressions to access the Order DOM instance. This example shows how data from the DOM instance is retrieved and substituted in the JSP:
viewOrder.jsp
<%@ page errorPage="error.jsp" %>
<%@ taglib uri="/src/tags/taglib.tld" prefix="mytag" %>
View Orders
OrderDate:
ShipTo:
ARTIST:
TITLE :
PRICE :
The code above uses three tags from the XPath tag library: getvalue , ifdef , and iterate . All three tags have a select attribute that specifies an XPath expression.
The getvalue applies the XPath expression specified in the select , on the Order DOM document instance and substitutes the result.
The conditional ifdef tag evaluates its body content only if the XPath expressions specified in its select are valid.
iterate , an iteration tag, parses its body contents for each iteration. In this case, the select attribute specifies a node set to be retrieved which is then iterated over. Note that the getvalue tag, when used within the iterate tag, specifies a relative XPath expression in its select attribute.
The example above covers simple data access, substitution, and control flow using XPath expressions in a JSP. The next example discusses attribute value substitution.
Example 2: Attribute value substitution
In our second example, the JSP uses XPath expressions to access the ShipTo data from the DOM instance, which is then edited using an HTML form. Note the use of the template: prefix for attribute value substitution:
editShipTo.jsp
<%@ taglib uri="/src/tags/taglib.tld" prefix="mytag" %>
.....
.....
.....
.....
The code above illustrates the use of the XPathTemplate container tag that enables XPath expression results to be substituted in attribute values. To achieve this, the template: keyword is used to prefix attribute values that need to be evaluated, as shown in the example above.
The XPathTemplate template tag parses its body content, looking for elements that have attributes starting with the template: prefix. When it finds one, it applies the XPath expression on the DOM document instance and rewrites the attribute and its value without the template: prefix. In the example above we saw how XPath expressions are used in attribute values and how the XPathTemplate tag is used for attribute value substitution. In the next section, we will see the various custom tags available as part of the XPath custom tag library.
The XPath custom tag library
We will now see the various tags and their attributes available in the XPath custom tag library. For each tag you'll find the name, description, and attribute details.
Tag: XPathTemplate
Description:
A container/super tag that enables XPath expressions in attributes
The body of this tag could be HTML or well-formed XML
For attribute-value substitution, the prefix template: is used on the attribute
For element substitution, the iterate tag or getvalue tag mentioned below are used
Attributes:
Attribute name: content
Attribute value: html (default) or xml
Tag: iterate
Description:
Allows iteration over an XPath expression that results in multiple nodes
Used in conjunction with the getvalue tag
Attributes:
Attribu
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Combine the power
of XPath and JSP
tag libraries -
JavaWorld January
2001
View Tutorial: Combine the power
of XPath and JSP
tag libraries -
JavaWorld January
2001
Related
Tutorials:
Boost Struts with
Boost Struts with XSLT and XML |
JSP Standard Tag Library eases Webpage
development
JSP Standard Tag Library eases Webpage
development |
Call JavaBean methods from JSP
Call JavaBean methods from JSP 2.0 pages |
JSP 2.0: The New Deal, Part 3
JSP 2.0: The New Deal, Part 3
More Flexible JSP Document Format Rules
The JSP specification supports two types of JSP pages: regular JSP pages containing any type of text or markup, and JSP Documents, which are well-formed XML documents; i.e., docum |
JSP 2.0: The New Deal, Part 4
JSP 2.0: The New Deal, Part 4
In this final part of the "JSP 2.0: The New Deal" series, we look at two new features that make it much easier to develop custom tag libraries: tag files and the new simplified tag-handler Java API. |
Building Java Server Pages
A detailed look at building JSP pages. Should you use JSP or servlets? It mainly depends on the ratio of markup to code. Here you'll also find a guide to the different varieties of tag, and details about the main tags such as and |
BEA WebLogic Portal JSP Tag Libraries
The BEA WebLogic Portal includes four JSP tag libraries that are used by the portal's JSP pages. |
JAVASERVER PAGESTM JAVASERVER PAGESTM
JSPTM tag libraries define declarative, modular functionality that can be reused by any JSP page. Tag libraries reduce the necessity to embed large amounts of Java code in JSP pages by moving the functionality provided by the tags into tag implementation |
JSPTags.com offers JSP developers a directory of resources.
JSPTags.com offers JSP developers a directory of resources related to JavaServer Pages, Servlets and Java. As the name JSPTags.com implies, special interest is given to JSP Tag Libraries. Many developers are working with and designing new JSP Tag Librarie |
JSP Tags
JSP tags do not use <%, but just the < character. A JSP tag is somewhat like an HTML tag. JSP tags can have a "start tag", a "tag body" and an "end tag". The start and end tag both use the tag name, enclosed in < and > characters. The end starts with |
Tag Libraries Tutorial
This tutorial describes how to use and develop JavaServer Pages tag libraries. The tutorial assumes that you know how to develop servlets and JSP pages and are familiar with packaging servlets and JSP pages into Web application archives. |
Jakarta Taglibs
This project is an open-source repository for JSP custom tag libraries and associated projects, such as TagLibraryValidator classes and extensions to page-creation tools to support tag libraries. |
Tutorial for Developing your first JSPs tags
We have seen how servlets and JSPs can be used to build a web application. These technologies go some distance toward making web development easier, but do not yet facilitate the separation of Java from HTML in a reusable way. Custom tags make this possib |
Compare JavaServer Pages: Tag Libraries vs. JavaBeans
Java provides developers with JavaServer Pages (JSPs) and Servlets as a superior alternative to traditional CGI programs. The architecture of JSPs provide support for a logical and physical separation between the HTML page designers and the component deve |
Encapsulate reusable functionality in JSP tags
JavaServer Pages (JSP) are a great mechanism for delivering dynamic Web-based content. JSP provides a set of predefined tags, but you can also define your own tag extensions that encapsulate common functionality. |
This tutorial shows how to Combine the power of XPath and JSP tag libraries
In this article, we'll examine the XPath custom tag library for JSPs and see a tag collection that provides simple control constructs and a uniform attribute value substitution facility, all of which combine to reduce complexity and improve functionality. |
This tutorial shows how to Creating Custom JSP Tag Libraries
JSP 1.1 introduced an extremely valuable new capability: the ability to define you own JSP tags. You Define how the tag, its attributes, and its body are interpreted, then group your tags into collections called tag libraries that can be used in any numbe |
Advanced Features of JSP Custom Tag Libraries
In this article, the second in the JSP custom tag libraries series, we will cover advanced JSP features and how to use them. |
Using Taglib in JSP. A brief introduction to taglibs and taglibs programing.
Using Taglib in JSP. A brief introduction to taglibs and taglibs programing.
JSP TAG LIBRARIES
JSP Tag Libraries :
JSP’s offer a unique feature of “Tag Libraries”. Simply put, these are custom defined JSP tags. They are basically meant for |
JavaServer Faces in Action, Chapter 8
Shows how to build a static Login page with JavaServer Faces and JSP technology by importing the proper tag libraries, and adding HtmlGraphicImage and HtmlOutputText components. |
|
|
|