Using JSTL with Struts

 < Day Day Up > 



Using JSTL with Struts is as simple as adding the JSTL .jar files (jstl.jar and standard.jar) to your Web application's library directory (/WEB-INF/lib) and then referencing the Tag Library Descriptors (.tlds) from your JSPs. There are two ways that you can reference JSTL .tlds in your JSPs. First, you can use an absolute URI to Sun's site, as shown next:

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

Second, you can employ the approach used by the Struts tag libraries: an entry is made in the web.xml file and then the URI assigned in the file is used by JSPs. If you choose to take this route, you need to copy the JSTL .tld files into your Web application's WEB-INF directory so that you can reference them.

The following table lists the absolute URI for each of the libraries, should you choose to reference the .tlds in that way.

Library

Prefix

URI

Core

c

http://java.sun.com/jstl/core

Format

fmt

http://java.sun.com/jstl/fmt

SQL

sql

http://java.sun.com/jstl/sql

XML

x

http://java.sun.com/jstl/xml

Struts-EL

As previously mentioned, with the advent of JSTL, the Struts tag library tags should now be used only when there is not a JSTL equivalent tag to replace them. This ensures that JSPs are as portable as possible and shields your application from being too heavily tied to Struts-specific facilities. The following table lists each of the Struts tag library tags that can be replaced by JSTL tags and their corresponding replacements.

Struts Tag Library

Tag

JSTL Replacement

Bean

cookie

c:set

Bean

define

c:set

Bean

header

c:set

Bean

include

c:import

Bean

parameter

c:set

Bean

write

c:out

Logic

empty

c:if, c:when

Logic

equal

c:if, c:when

Logic

greaterEqual

c:if, c:when

Logic

greaterThan

c:if, c:when

Logic

iterate

c:forEach

Logic

lessEqual

c:if, c:when

Logic

lessThan

c:if, c:when

Logic

notEmpty

c:if, c:when

Logic

notEqual

c:if, c:when

As you can see, JSTL can be used in lieu of many of the Struts tag library tags. However, you may have noticed that none of the tags from the Struts HTML Tag Library has an equivalent JSTL tag. JSTL does not have a tag library for rendering HTML form elements, thus the absence of Struts tag replacements. For the HTML tags and all the tags in the Bean and Logic tag libraries that do not have JSTL replacements, a project called Struts-EL was created. Struts-EL was created by David Karr and is currently distributed with Struts in the distribution's contrib folder. It is likely that Struts-EL will eventually become part of the Struts core.

The Struts-EL project is an extension to Struts that provides a JSTL expression language-enabled version of each Struts tag for which no JSTL replacement exists. Most of the base Struts tag library tags' attributes accept values represented as scriptlet expressions. This allows the tags to have dynamic attribute values. For example, the Bean Tag Library's message tag accepts scriptlet expressions for its key attribute, as shown here:

<bean:message key="<%=messageKey%>"/>

This example uses the value of the messageKey JSP scripting variable as the value for the message tag's key attribute. Notice that the JSP scripting variable had to be specified within the scriptlet expression identifiers <%= and %>.

The following example shows the Struts-EL equivalent of the previous example using a JSTL expression to specify a dynamic value for the key attribute:

<bean-el:message key="${messageKey}"/>

As you can see, the JSTL expression syntax is a little shorter and is cleaner looking.

As mentioned, the basic concepts of using JSTL expressions apply to all the Struts-EL tags in the same way. Any tag attribute that accepts a scriptlet expression with the base tags will accept a JSTL expression with the Struts-EL tags.

JSTL Replacement Examples

The following sections provide examples for replacing Struts tag library tags with their JSTL equivalents. Remember that not all the Bean, HTML, and Logic tags can be replaced by JSTL tags.

bean:cookie Replacement Example

The following snippet shows the basic usage of the cookie tag from the Bean Tag Library:

<bean:cookie  name="cat"/>

The JSTL equivalent is as follows:

<c:set var="category" value="${cookie['cat'].value}"/>

This example accesses the cat cookie with a JSTL expression that makes use of the JSTL implicit cookie object.

bean:define Replacement Example

The following snippet shows the basic usage of the define tag from the Bean Tag Library:

<bean:define  name="nameObj"/>

The JSTL equivalent is as follows:

<c:set var="name" value="${nameObj}"/>

bean:header Replacement Example

The following snippet shows the basic usage of the header tag from the Bean Tag Library:

<bean:header  name="User-Agent"/>

The JSTL equivalent is as follows:

<c:set var="browser" value="${header['User-Agent']}"/>

This example accesses the 'User-Agent' header with a JSTL expression that makes use of the JSTL implicit header object.

bean:include Replacement Example

The following snippet shows the basic usage of the include tag from the Bean Tag Library:

<bean:include  href="http://www.yahoo.com/"/>

The JSTL equivalent is as follows:

<c:import var="yahooContents" url=" http://www.yahoo.com/"/>

bean:parameter Replacement Example

The following snippet shows the basic usage of the parameter tag from the Bean Tag Library:

<bean:parameter  name="clr"/>

The JSTL equivalent is as follows:

<c:set var="color" value="${param['clr']}"/>

This example accesses the clr parameter with a JSTL expression that makes use of the JSTL implicit param object.

bean:write Replacement Example

The following snippet shows the basic usage of the write tag from the Bean Tag Library:

<bean:write name="bizObj"/>

The JSTL equivalent is as follows:

<c:out value="${bizObj}" />

logic:empty Replacement Example

The following snippet shows the basic usage of the empty tag from the Logic Tag Library:

<<logic:empty name="results"> Your search yielded no results. </logic:empty>

The JSTL equivalent is as follows:

<c:if test="${empty results}"> Your search yielded no results. </c:if> 

logic:equal Replacement Example

The following snippet shows the basic usage of the equal tag from the Logic Tag Library:

<logic:equal name="count" value="0"> Count is zero. </logic:equal>

The JSTL equivalent is as follows:

<c:if test="${count == 0}"> Count is zero. </c:if>

bean:greaterEqual Replacement Example

The following snippet shows the basic usage of the greaterEqual tag from the Logic Tag Library:

<logic:greaterEqual name="count" value="5"> Count is greater than or equal to five. </logic:greaterEqual>

The JSTL equivalent is as follows:

<c:if test="${count >= 5}"> Count is greater than or equal to five. </c:if>

logic:greaterThan Replacement Example

The following snippet shows the basic usage of the greaterThan tag from the Logic Tag Library:

<logic:greaterThan name="count" value="5"> Count is greater than five. </logic:greaterThan>

The JSTL equivalent is as follows:

<c:if test="${count > 5}"> Count is greater than five. </c:if>

logic:iterate Replacement Example

The following snippet shows the basic usage of the iterate tag from the Logic Tag Library:

<logic:iterate  collection="<%=results%>"> Result: <%=result%><br> </logic:iterate>

The JSTL equivalent is as follows:

<c:forEach var="result" items="${results}"> Result: <c:out value="${result}"/> </c:forEach>

logic:lessEqual Replacement Example

The following snippet shows the basic usage of the lessEqual tag from the Logic Tag Library:

<logic:lessEqual name="count" value="5"> Count is less than or equal to five. </logic:lessEqual>

The JSTL equivalent is as follows:

<c:if test="${count <= 5}"> Count is less than or equal to five. </c:if>

logic:lessThan Replacement Example

The following snippet shows the basic usage of the lessThan tag from the Logic Tag Library:

<logic:lessThan name="count" value="5"> Count is less than five. </logic:lessThan>

The JSTL equivalent is as follows:

<c:if test="${count < 5}"> Count is less than five. </c:if>

logic:notEmpty Replacement Example

The following snippet shows the basic usage of the notEmpty tag from the Logic Tag Library:

<logic:notEmpty name="results"> Your search returned results! </logic:notEmpty> 

The JSTL equivalent is as follows:

<c:if test="${!empty results}"> Your search returned results! </c:if>

logic:notEqual Replacement Example

The following snippet shows the basic usage of the notEqual tag from the Logic Tag Library:

<logic:notEqual name="count" value="0"> Count is not equal to zero.  </logic:notEqual>

The JSTL equivalent is as follows:

<c:if test="${count != 0}"> Count is not equal to zero.  </c:if>

Using the Struts-EL Tag Libraries

To use the Struts-EL tag libraries in a Struts application, you need to include the following snippet in your Web Archive (.war) deployment descriptor, web.xml:

<taglib>   <taglib-uri>/WEB-INF/struts-bean-el.tld</taglib-uri>   <taglib-location>/WEB-INF/struts-bean-el.tld</taglib-location> </taglib> <taglib>   <taglib-uri>/WEB-INF/struts-html-el.tld</taglib-uri>   <taglib-location>/WEB-INF/struts-html-el.tld</taglib-location> </taglib> <taglib>   <taglib-uri>/WEB-INF/struts-logic-el.tld</taglib-uri>   <taglib-location>/WEB-INF/struts-logic-el.tld</taglib-location> </taglib>

Of course, if you only want to use one or two of the libraries, you could place just their taglib definitions in web.xml.

Recall from the overview of the web.xml file in Chapter 2 that <taglib-uri> is used to declare the URI (or alias) that will be referenced in each of your JSPs with a taglib directive. The <taglib-location> tag declares the actual location of the Tag Library Descriptor (.tld) file in your Web Archive.

The following snippet illustrates how your JSPs will declare their use of the Struts-EL tag libraries with JSP taglib directives:

<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %> <%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %> <%@ taglib uri="/WEB-INF/struts-logic-el.tld" prefix="logic-el" %>

Notice that the uri attributes specified here are the same as those declared with the <taglib-uri> tags in the web.xml file. Also, notice that the prefix attributes are set to 'bean-el', 'html-el', and 'logic-el', respectively. These attributes can be set to whatever you want; however, 'bean-el', 'html-el', and 'logic-el' are the accepted defaults for the Struts-EL tag libraries. The prefix attribute declares the prefix that each tag must have when it is used in the JSP, as shown here:

<bean-el:message key="label.search.name">

Because 'bean-el' was defined as the prefix, the message tag was used as shown. However, if you chose to use a prefix of 'strutsbean-el', the tag would be used in the following way:

<strutsbean-el:message key="label.search.name">

The Struts-EL Tag Library Tags

As mentioned, each of the tags in the Struts-EL tag libraries is simply a tag from the Bean, HTML, Logic, or Tiles tag libraries to which support for JSTL expressions has been added. Thus, they are not individually covered in detail here. Instead, the basic concepts of using JSTL expressions with the tags were discussed because they apply to all the extended tags in the same way. For non-Struts-EL-related information on each of the extended tags, see the descriptions of their base tags in their respective chapters.

The remainder of this section lists each Struts-EL library-specific tag and the base tag from which it has been extended. Remember that not all of the Bean, HTML, Logic, and Tiles tags have been extended to support JSTL expressions. Only those tags whose functionality cannot be wholly replaced by a JSTL tag have been extended.

The Struts-EL Bean Tag Library Tags

The following table lists each of the tags in the Struts-EL Bean Tag Library and provides a short description of each tag's purpose.

Tag

Description

include

Expression language-enabled version of the Bean Tag Library's include tag.

message

Expression language-enabled version of the Bean Tag Library's message tag.

page

Expression language-enabled version of the Bean Tag Library's page tag.

resource

Expression language-enabled version of the Bean Tag Library's resource tag.

size

Expression language-enabled version of the Bean Tag Library's size tag.

struts

Expression language-enabled version of the Bean Tag Library's struts tag.

The Struts-EL HTML Tag Library Tags

The following table lists each of the tags in the Struts-EL HTML Tag Library and provides a short description of each tag's purpose.

Tag

Description

base

Expression language-enabled version of the HTML Tag Library's base tag.

button

Expression language-enabled version of the HTML Tag Library's button tag.

cancel

Expression language-enabled version of the HTML Tag Library's cancel tag.

checkbox

Expression language-enabled version of the HTML Tag Library's checkbox tag.

errors

Expression language-enabled version of the HTML Tag Library's errors tag.

file

Expression language-enabled version of the HTML Tag Library's file tag.

form

Expression language-enabled version of the HTML Tag Library's form tag.

frame

Expression language-enabled version of the HTML Tag Library's frame tag.

hidden

Expression language-enabled version of the HTML Tag Library's hidden tag.

html

Expression language-enabled version of the HTML Tag Library's html tag.

image

Expression language-enabled version of the HTML Tag Library's image tag.

img

Expression language-enabled version of the HTML Tag Library's img tag.

javascript

Expression language-enabled version of the HTML Tag Library's javascript tag.

link

Expression language-enabled version of the HTML Tag Library's link tag.

messages

Expression language-enabled version of the HTML Tag Library's messages tag.

multibox

Expression language-enabled version of the HTML Tag Library's multibox tag.

option

Expression language-enabled version of the HTML Tag Library's option tag.

options

Expression language-enabled version of the HTML Tag Library's options tag.

optionsCollection

Expression language-enabled version of the HTML Tag Library's optionsCollection tag.

password

Expression language-enabled version of the HTML Tag Library's password tag.

radio

Expression language-enabled version of the HTML Tag Library's radio tag.

reset

Expression language-enabled version of the HTML Tag Library's reset tag.

rewrite

Expression language-enabled version of the HTML Tag Library's rewrite tag.

select

Expression language-enabled version of the HTML Tag Library's select tag.

submit

Expression language-enabled version of the HTML Tag Library's submit tag.

text

Expression language-enabled version of the HTML Tag Library's text tag.

textarea

Expression language-enabled version of the HTML Tag Library's textarea tag.

xhtml

Expression language-enabled version of the HTML Tag Library's xhtml tag.

The Struts-EL Logic Tag Library Tags

The following table lists each of the tags in the Struts-EL Logic Tag Library and provides a short description of each tag's purpose.

Tag

Description

forward

Expression language-enabled version of the Logic Tag Library's forward tag.

iterate

Expression language-enabled version of the Logic Tag Library's iterate tag.

match

Expression language-enabled version of the Logic Tag Library's match tag.

messagesNotPresent

Expression language-enabled version of the Logic Tag Library's messagesNotPresent tag.

messagesPresent

Expression language-enabled version of the Logic Tag Library's messagesPresent tag.

notMatch

Expression language-enabled version of the Logic Tag Library's notMatch tag.

notPresent

Expression language-enabled version of the Logic Tag Library's notPresent tag.

present

Expression language-enabled version of the Logic Tag Library's present tag.

redirect

Expression language-enabled version of the Logic Tag Library's redirect tag.

The Struts-EL Tiles Tag Library Tags

The following table lists each of the tags in the Struts-EL Tiles Tag Library and provides a short description of each tag's purpose.

Tag

Description

add

Expression language-enabled version of the Tiles Tag Library's add tag.

definition

Expression language-enabled version of the Tiles Tag Library's definition tag.

get

Expression language-enabled version of the Tiles Tag Library's get tag.

getAsString

Expression language-enabled version of the Tiles Tag Library's getAsString tag.

importAttribute

Expression language-enabled version of the Tiles Tag Library's importAttribute tag.

initComponentDefinitions

Expression language-enabled version of the Tiles Tag Library's initComponentDefinitions tag.

insert

Expression language-enabled version of the Tiles Tag Library's insert tag.

put

Expression language-enabled version of the Tiles Tag Library's put tag.

putList

Expression language-enabled version of the Tiles Tag Library's putList tag.

useAttribute

Expression language-enabled version of the Tiles Tag Library's useAttribute tag.



 < Day Day Up > 



Struts. The Complete Reference
Struts: The Complete Reference, 2nd Edition
ISBN: 0072263865
EAN: 2147483647
Year: 2003
Pages: 134
Authors: James Holmes

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