A crash course on JSTL


JSTL stands for JSP Standard Template Library. It is one of the new specifications from Sun for standardizing common tags. Due to the lack of standard tags for most of the common tasks such as iterating over a collection and displaying it as a table, custom tags from different vendors have sprung up (including Struts Logic tags), thus presenting a formidable learning curve for the developers every time a new vendor is chosen . JSTL has standardized the set of tags. This standardization lets you learn a single tag and use it across the board. Table 6.1 shows the JSTL tag categories. Core and Formatting tags are most relevant to the discussion on Struts.

Table 6.1: JSTL Libraries

Library

Description

Core

Contains Core tags for if/then, output, iterating collections,

Formatting

Contains I18N and formatting tags. Localizing text, Setting Resource Bundles, Formatting and parsing number, currency, date

SQL

Database Access tags

XML

Tags for XML Parsing and Transforming with Xpath

JSTL also introduced a new expression language (called EL henceforth) as an alternative for using full-blown JSP expressions. For e.g. consider the following scriptlet. It checks for the ‚“user ‚½ in page, request, session and application scopes and if it is not null, prints out the roles of that user .

 <%          User user = (User) (pageContext.findAttribute("user");         if (user != null) {             Role[] roles = user.getRoles();     %>     <ul>            <% for (int i=0;i<roles.length;i++) { %>               <li>Role Name is <%= roles[i].getName() %></li>            <% }%>         <% }%>     </ul> 

This can be easily written using JSTL and EL as follows :

 <ul>         <c:forEach items=${user.roles} var=role>             <li><c:out value=${role.name}/></li>         </c:forEach>         </ul> 

Any value to be evaluated in a JSTL tag lies in ${ and } blocks. EL defines several implicit objects much like JSP defines its implicit objects. Table 6.2 gives a complete list of implicit objects. If the name of the variable in the ${ and } block does not match one of the implicit objects, then EL searches the page, request, session application scopes in that order for the variable name specified. In the above code snippet, ‚“user ‚½ is the name of an attribute in one of these scopes. Once the < c:forEach > tag gets the value, it iterates over the specified collection. In this case it iterates over the array of roles and provides a scripting variable called role ( var= ‚½role ‚½ ) for the embedded tags. The < c:out > tag access the name property of the Role object (obtained from the role scripting variable) and renders it. The c in the < c:out > represents the Core JSTL tag library.

Table 6.2: Implicit objects in EL

Category

Identifier

Description

JSP

pageContext

PageContext for the current page

Scope

pageScope

requestScope

sessionScope

ApplicationScope

Map holding page scoped attributes

Map holding request scoped attributes

Map holding session scoped attributes

Map holding application scoped attributes

Request Parameters

Param

ParamValues

Map holding request parameter names

Map holding request parameter values as arrays

Request headers

Header

HeaderValues

Map holding header names

Map holding header values

Cookies

Cookie

Map holding cookies by name

Initialization Parameters

InitParams

Map holding web application context initialization parameters by name

Note ‚  

JSTL 1.0 works with JSP 1.2 containers only, such as Tomcat 4.x. JSTL 1.1 works only with JSP 2.0 containers such as Tomcat 5.x. With JSP 1.2, the expression language can be used only within JSTL tags. JSP 2.0 specification defines a portable expression language. With JSP 2.0, the expression language will become part of the specification and can be used even outside the JSTL.

You have already seen an example of using JSTL Core library earlier in conjunction with EL. Now, let us look at an example of formatting library tags. Consider the case when you want to display the currency 12.37 in the user ‚ s Locale. You can use the formatNumber tag for this purpose. In the following example the currency is formatted and displayed in a variable called ‚“money ‚½. For the U.S. Locale, money will contain the value ‚“$12.37 ‚½.

 <fmt:formatNumber value="12.367" type="currency" var="money"/> 

This is somewhat similar to the < bean:write > tag in terms of formatting the currency. Similarly there is a JSTL equivalent for < bean:message > tag.

 <fmt:message key="firstName"> 

In JSTL, the Resource Bundle for the above tag can be specified in a number of ways. Unless specified otherwise , JSTL looks for a servlet context parameter named javax.servlet.jsp.jstl.fmt.localizationContext and uses its value as the bundle name. You can also use the tag < fmt:setBundle baseName= ‚½mybank.MyMessages ‚½ > in JSP and the rest of the JSP uses the specified bundle. You can scope a bundle by wrapping other tags with < fmt:bundle > tag as follows:

 <fmt:bundle baseName=mybank.MySecondMessages>            <fmt:message key="firstName">            <fmt:message key="lastName">         </fmt:bundle> 

In the above snippet, all the inner tags use mybank.MySecondMessages as their resource bundle. The resource bundle lookup is similar to Struts. In the above scenario for instance, the servlet container looks for MySecondMessages.properties in WEB-INF/classes/mybank and then in the system classpath.

Design Tip

Since, Struts allows you to specify resource bundles on a tag basis, it seems easier (and logical) to use separate bundles per category. For instance, all the errors can reside in one bundle and all the messages of one type can reside in one bundle and so on.

JSTL on the other hand seems to encourage the practice of using resource bundle per module. This is evident from the way you specify the bundles at a JSP level, scope it and so on. It is easier this way to use a resource bundle for a set of JSPs belonging to one module.

 

JSTL Binaries ‚ Who ‚ s who

If you download JSTL Reference Implementation from Sun, it has two important jar files ‚ jstl.jar and standard.jar . The former contains the classes from javax.servlet.jsp.jstl package and the latter contains Sun ‚ s JSTL Reference Implementation.

From a perspective of this book, we will be using Struts-EL, the JSTL compliant port of Struts tags. Struts-EL is shipped with Struts 1.1 release as a contributed library and can be found under the contrib folder. Struts-EL uses the same jstl.jar containing javax.servlet.jsp.jstl package ‚ it is the vendor independent JSTL standard. However it uses the implementation from Jakarta TagLibs as the underlying expression evaluation engine (This implementation is also named standard.jar and found under Struts-EL/lib ). If you explode the standard.jar , you will find classes belonging to org.apache.taglibs package.




Struts Survival Guide. Basics to Best Practices
Struts Survival Guide: Basics to Best Practices (J2ee Survival Series)
ISBN: 0974848808
EAN: 2147483647
Year: 2004
Pages: 96

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