JSTL: The Standard Tag Library


As developers have created more and more custom tag libraries, they discovered a need to come up with a standard library of the most commonly used ones. This led to the Java Standard Tag Library standard (JSTL).

JSTL can be broken up into a number of large sections. Each one will be briefly summarized in the following sections.

General Purpose Actions

The general purpose tags are c.out , which writes a value to the JSP stream; c.set , which sets a scoped variable; c.remove , which removes a scoped variable; and c.catch , which is used to catch exceptions inside its body.

Conditional Actions

The conditional action tags implement control flow. c:if is a straightforward conditional evaluation, whereas c:choose in combination with the c:when and c: otherwise tags implements a flow similar to the switch statement.

Iterator Actions

The other half of flow control, these tags define the looping constructs for JSTL. The c:forEach tag will loop either over a collection of objects or for a certain number of times. The c:forTokens tag uses a delimiter character to break a string into pieces, and then iterates over the pieces.

URL-Related Actions

These actions relate to Web pages. The c:import tag causes another Web page to be inserted at this point in the JSP document. Using c:url , a relative URL will be correctly rewritten as an absolute one. Finally, c:redirect causes a redirect to another page. All these actions can use a c:param tag inside them to pass a parameter to the new page.

Internationalization Actions

Using these tags, Web content can be internationalized. Using fmt:setLocale , the locale of the page can be altered . The fmt:setBundle and fmt:bundle tags let multiple message resource bundles be used on a page. After a bundle is available, the fmt:message tag is used to look up the specific message. Finally, the fmt:requestEncoding tag enables the developer to change the character encoding used on the page.

Formatting Actions

This set of tags handles common formatting requirements. The fmt:timeZone and fmt:setTimeZone tags are used to establish the correct time zone, which is used by fmt:formatDate and fmt:parseDate . The fmt:formatNumber and fmt:parseNumber tags supply similar functions for numbers .

SQL Actions

By using these tags, actions can be taken against a database. After using sql:setDataSource to gain access to a database, the sql:query and sql:update tags can be used to read and write from it. If transaction control is needed, it's available from the sql:transaction tag.

XML Actions

The final set of standard tags is used to work with XML files. The x:parse tag parses an XML file. After the file is parsed, the x:out tag will send XML data to the JspWriter, whereas x:set can be used to set variables to XML values.

The XML tags also include a set of control tags ( x:if , x:choose , x:when , x:otherwise , and x:forEach ) that work similarly to the control flow tags, but for XML data.

Finally, the x:transform tag can be used to transform an XML document to HTML using an XSLT stylesheet.

Scripting Language

In addition to all the new tags, JSTL also introduces an entire scripting language intended to allow the same degree of functionality as is currently available using Java scriptlets but in the normalized form of tags. Some developers consider the scripting language to be the most powerful piece of the emerging JSTL standard.

These descriptions are meant to serve only as a basic introduction to what JSLT offers. They can be explored through the online specifications available at java.sun.com .



Struts Kick Start
Struts Kick Start
ISBN: 0672324725
EAN: 2147483647
Year: 2002
Pages: 177

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net