Recipe3.2.Using the Struts-EL Tags


Recipe 3.2. Using the Struts-EL Tags

Problem

You want to be able to use JSTL expressions for attribute values on Struts tags.

Solution

Use the tag libraries supplied with the Struts distribution in the contrib/struts-el/lib directory. You will need to copy all the JAR and TLD files from this directory to your application's WEB-INF/lib directory. Use the appropriate taglib directives on JSP pages where you want to use expressions:

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html-el"          prefix="html-el" %>

Table 3-2 lists the Struts-EL tag libraries and the corresponding taglib URIs.

Table 3-2. Struts-EL Taglib URIs

Tag library

Struts-EL Taglib URI (1.1)

Struts-EL Taglib URI (1.2)

html-el

http://jakarta.apache.org/struts/tags-html-el

http://struts.apache.org/tags-html-el

bean-el

http://jakarta.apache.org/struts/tags-bean-el

http://struts.apache.org/tags-bean-el

logic-el

http://jakarta.apache.org/struts/tags-logic-el

http://struts.apache.org/tags-logic-el


Discussion

JSTL-style expressions, such as ${foo.bar[4].baz}, are not supported by the base Struts tags. For example, it would be nice if you could format a tag using an expression like the following:

<html:text value="${sessionScope.foo.bar[3]}"/>

Instead, these tags require runtime expressions, which is just Java code:

<html:text      value="<%=session.((Foo)getAttribute("foo")).getBar(3)%>"/>

Getting the Java code out of your JSP pages makes your pages less brittle and more maintainable. This lack of EL support was identified and the Struts-EL tag libraries were created. These libraries extend the html, bean, and logic Struts tag libraries to add support for EL expressions. If an attribute of a Struts tag supports a runtime expression, the corresponding Struts-EL tag will allow a JSTL expression. It is possible to use the regular Struts tags and the Struts-EL tags in the same application and even on the same JSP page. Just be sure to define unique prefixes in the taglib directive for each library.

The Struts-EL tags are not a replacement, however, for JSTL. The Struts-EL tags only provide unique tags for Struts. If a Struts tag can be replaced by a JSTL tag, that tag is not implemented in the Struts-EL tag libraries.

Looking to JSP 2.0

If you are using a container that supports JSP 2.0, such as Tomcat 5, then you can use EL expressions in the base Struts tags. You don't need to use the Struts-EL tags. JSP 2.0 supports the use of EL expressions directly on a JSP page. These expressions can be used to render dynamic text, anywhere on the page, as ordinary text within conventional HTML markup as well as dynamic attribute values for custom JSP tags. The value of the expression is output just as if you had used the JSTL c:out tag:

<p>Hello, ${user.firstName}<br /> <html:text value="${sessionScope.foo.bar[3]}"/>

To make use of this powerful feature, you must enable EL using one of two approaches. If your web application uses the Servlet 2.4 specification, then EL is enabled by default. You can tell if you are using the 2.4 spec by looking at your application's web.xml file. The beginning of the file will look something like this:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.     sun.com/xml/ns/j2ee/web-app_2_4.xsd"     version="2.4">

If you are using Version 2.3 or earlier, then you will need to set the following JSP directive at the beginning of your JSP page:

<%@page isELIgnored="false"%>

If you are using the 2.4 DTD, then you do not need to set the page directive; EL syntax will not be ignored. If you are unsure or are having problems, go ahead and set the page directive; it won't cause any problems.


See Also

Recipe 3.1 details how to configure your application to use JSTL.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

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