8.9 The JSP Standard Tag Library (JSTL)


The intent of the early JSP specification group members was to define a set of standard JSP tags within the specification. This would allow vendors to generate their own versions of the standard tags but also allow developers to count on this set of standard tags to be available in all compliant containers. However, due to time constraints, that feature of the early JSP specification was not included.

Since then, many vendors have created their own versions of commonly used tag libraries, but these versions are different enough that developers can't easily port their JSP pages from one container to another without having to modify the pages. JSR 52, the JSP Standard Tag Library (JSTL), aims to fix that problem.

JSR 52 defines a set of standard tags that should be present in any compliant container. The second version of the specification has been finalized and approved, and the group has released a reference implementation of the tag library.

The tags provided in the first release can be grouped into five distinct areas:

  • Core tags

  • Internationalization tags

  • XML tags

  • SQL tags

  • Functions

8.9.1 JSTL Core Tags

The core tags include those related to expressions, flow control, and a generic way to access URL-based resources whose content can then be included or processed within the JSP page. They include tags such as if, forEach, import, redirect, and many more.

8.9.2 JSTL Internationalization Tags

The internationalization tags are divided into two groups: messaging and formatting.

The messaging tags assist page authors with creating messages that can be adapted to any locale available in the JSP container. They include tags such as bundle and message.

The formatting tags allow various data elements, such as numbers, currencies, dates, and times, to be formatted and parsed in a locale-sensitive manner. They include tags such as formatDate, parseNumber, and timeZone.

8.9.3 JSTL XML Tags

The JSTL XML tag library includes tags that allow XML documents to be accessed. The tags are based on XPath. The XML tags use XPath as a local expression language.

8.9.4 JSTL SQL Tags

The JSTL SQL tags allow direct access to JDBC resources. They are designed for quick prototyping and simple applications. For larger, more advanced applications, database operations should not be present in the presentation layer they are normally performed in the model layer. This helps to ensure separation of responsibility.

The tags within the JSTL SQL library include tags for configuring the JDBC resource as well as for querying and updating the database.

8.9.5 Functions

Functions allow developers to extend the capabilities of the JSP 2.0 expression language (EL), which is briefly mentioned in the next section. The JSTL functions are grouped together within the Functions tag library.

The Functions tag library contains two essential areas of interest; one if the length function for determining the size of collections. The second area of focus of the Functions tag library is providing the ability for String manipulation. Both of these capabilities was missing from the original specification.

8.9.6 A New Expression Language

JSTL also introduce a new expression language (EL) to make it easier for page authors to access application data without forcing them to learn a full-fledged programming language like Java. The EL is part of JSR 152 (the JSP 1.3 specification), but it has been included with JSTL to ensure that a specification is available for the JSTL schedule. The two groups are working closely together so that the EL fulfills both sets of needs.

The JSTL tag libraries come in two versions. One version, known as the JSTL-RT, will continue to support expressions as they are used today. The second version, known as JSTL-EL, will support the new expression language. Both versions will be supported.

For more information on the EL, refer to the JSTL 1.1 specification, which can be downloaded from http://jcp.org/jsr/detail/52.jsp.

8.9.7 JSTL Requirements

JSTL requires a servlet container that supports the Servlet 2.3 specification and Version 1.2 of the JSP specification. If you are using a container that doesn't support these specifications, you will not be able to use JSTL. Tomcat is one container that supports both, but there are many more. Make sure to check with your vendor before attempting to use JSTL.

8.9.8 JSTL and Struts

The Struts Bean and Logic tag libraries may eventually be phased out in favor of the JSTL tags. There already have been changes within Struts to better support JSTL and EL. In the next several versions of the Struts framework, look for more changes to help ease that migration.

It should be obvious from looking at the tags included with JSTL that not all of the Struts tags are being replaced. The tags within the HTML tag library in particular will be around for some time.



Programming Jakarta Struts
Programming Jakarta Struts, 2nd Edition
ISBN: 0596006519
EAN: 2147483647
Year: 2003
Pages: 180

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