Struts-EL


As you might know already, Struts-EL is a port of Struts tags to JSTL. This provides a migration path for the existing Struts applications to the expression language syntax in a non- intrusive manner. Normal Struts tags rely on runtime scriptlet expressions to evaluate dynamic attribute values. For example, the key of the bean:message below is dependent on some business logic.

 <bean:message key=<%= stringVar %> /> 

This assumes that stringVar exists as a JSP scripting variable. This tag can be rewritten with the Struts-EL version of the message tag as follows :

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

Although, not much exciting is going on in the above tag, it shows how easy it is to port the existing Struts tags to Struts-EL. The real power of Struts-EL comes to the fore especially when the scriptlet deciding the attribute value starts becoming complex.

Not all tags from Struts are ported to Struts-EL. In areas where there is already a JSTL tag available, porting of the Struts tags will only cause redundancy. Hence those Struts tags are not ported. For e.g., the bean:write tag can be implemented with the c:out JSTL tag. Similarly most of the logic tags (such as equal , notEqual , lessThan etc.) are not ported since the JSTL tag c:if can take any expression and evaluate it (with the test= ‚½${ ‚ .} ‚½ option). You have already seen how a logic:equal tag can be replaced with c:if in the earlier section on Nested Logic Tags.

Struts-EL hands-on

Enough theory. Let ‚ s get down to business and use some Struts-EL tags to get the feel. Here is the step-by-step process to do so.

  • You will need new jar files to use the Struts-EL in your application. Copy the following jars from the Struts contrib folder into the WEB-INF/lib folder of the web application ‚ jstl.jar , standard.jar (remember to use the Jakarta Taglibs version, not the Sun reference implementation jar), struts-el.jar . These jars are needed in addition to the already existing jars from regular Struts.

  • From the Struts-EL/lib folder copy the following tlds to the WEB-INF of your web application ‚ c.tld , struts-bean-el.tld , struts-html-el.tld and struts-logic-el.tld .

  • Add the < taglib > declaration for all the new tlds in web.xml as follows:

     <taglib>   <taglib-uri>/WEB-INF/struts-bean-el</taglib-uri>   <taglib-location>/WEB-INF/struts-bean-el.tld</taglib-location> </taglib> <taglib>   <taglib-uri>/WEB-INF/struts-html-el</taglib-uri>   <taglib-location>/WEB-INF/struts-html-el.tld</taglib-location> </taglib> <taglib>   <taglib-uri>/WEB-INF/struts-logic-el</taglib-uri>   <taglib-location>/WEB-INF/struts-logic-el.tld</taglib-location> </taglib> <taglib>   <taglib-uri>/WEB-INF/c</taglib-uri>   <taglib-location>/WEB-INF/c.tld</taglib-location> </taglib> 
  • In the JSPs, add the declaration for these TLDs as follows:

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

That ‚ s it! Now you are ready to use the Struts-EL tags in conjunction with JSTL tags to reap the benefits of expression language and make your applications a little bit simpler and cleaner.

Practical uses for Struts-EL

When was the last time you wrestled to use a custom tag as the attribute value of another tag and failed? Something like this:

 <html:radio name=anotherbean     value=<bean:write name=mybean property=myattrib/> /> 

Nesting custom tag within a tag element is illegal by taglib standards. The alternatives are no good. Thankfully now, with JSTL, you can solve this problem in a clean way. In Struts tags, JSTL can be combined only with Struts-EL and the problem can be solved as follows:

 <html-el:radio name=anotherbean value=${mybean.myattrib} /> 

Beautiful isn ‚ t it! Struts-EL provides you the best of both worlds , the elegance of JSTL and the power of Struts.




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