|
Recipe 3.2. Using the Struts-EL TagsProblemYou want to be able to use JSTL expressions for attribute values on Struts tags. SolutionUse 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.
DiscussionJSTL-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.
See AlsoRecipe 3.1 details how to configure your application to use JSTL. |
|