November 26, 2014
Hot Topics:

Apache Struts Framework: The Big Picture

  • September 26, 2005
  • By Vlad Kofman
  • Send Email »
  • More Articles »

For example, here is web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//
   DTD Web Application 2.3//EN"
   "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
   <servlet>
      <servlet-name>action</servlet-name>
      <servlet-class>
         org.apache.struts.action.ActionServlet
      </servlet-class>
      <init-param>
         <param-name>application</param-name>
         <param-value>com.myapp.action.ResourceFile</param-value>
      </init-param>
      <init-param>
         <param-name>config</param-name>
         <param-value>/WEB-INF/struts-config.xml</param-value>
      </init-param>
      <init-param>
         <param-name>debug</param-name>
         <param-value>2</param-value>
      </init-param>
      <load-on-startup>2</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>action</servlet-name>
      <url-pattern>*.do</url-pattern>
   </servlet-mapping>
   <session-config>
      <session-timeout>30</session-timeout>
   </session-config>
   <taglib>
      <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
      <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
   </taglib>
   <taglib>
      <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
      <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
   </taglib>
   <taglib>
      <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
      <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
   </taglib>
   <taglib>
      <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
      <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
   </taglib>
   <taglib>
      <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
      <taglib-location>/WEB-INF/struts-template.tld</taglib-location>
   </taglib>
   <taglib>
      <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
      <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
   </taglib>
</web-app>
Note: *.do is mapped to the action servlet, which means that any request to any action that ends with ".do" will go through the main controller servlet first.

The web.xml includes a definition of yet another great Strut's feature—a vast collection of pre-built tag libraries.

Bean tags contain JSP custom tags to manipulate java beans in any application scope, as well as render of them to the output.

Custom HTML tags include numerous form inputs, dynamic user interfaces, and other very useful tags.

Logic tags deal with application flow management, such as iteration over collections and generation of output.

I welcome you to read more about all included tags on the Struts Web site http://struts.apache.org/. All of these tags help to reduce the amount of Java scriplet code in the JSP, and they can be used with the Java Standard Tag Library (JSTL) or any other custom tags and are extremely useful. In addition, because Struts is one of the most popular frameworks, all these tags have been mapped as Macromedia Dreamweaver (or Ultra-Dev) plug-ins so that HTML developers and designers can use them without knowing anything about Java.

Another tremendously helpful and efficient feature of Struts is resource files and internalization. It is very likely that any Web application will have some components that appear in more then one place throughout the application. Labels, button names, page comments, titles, headers or footers, as well as error messages can be included as a property recourse file in any Struts application. All that needs to be done is to add a text properties file with name-value pairs placed in the application path, and referenced as a parameter to the Struts controller servlet.

Struts also supports internalization by reading the user locale, which arrives as part of the HTTP request. It can use different properties files for different locales and match them to the user responses. For example, if ResourceFile_it file exists and the user's request identifies it as Italian, this resource file will be used instead of the default one.

Validation

The Struts framework provides a very clever way to validate user input fields and comes with pre-built modules for it. The first thing needed for the setup of validation is a custom validation.xml file, and a Struts-provided validator-rules.xml file. Both of these files need to be in the same WEB-INF dir as the web.xml and struts-config.xml files. The validation.xml file needs to be included in the struts-config file as well.

Here is the example of validation.xml file.

<form-validation>
   <formset>
      <form name="someForm">
      <field property="text"
             depends="required,minlength">
         <arg0 key="prompt.text"/>
         <arg1 name="minlength"
            key="${var:minlength}" resource="false"/>
         <var>
            <var-name>minlength</var-name>
            <var-value>0</var-value>
         </var>
      </field>
      </form>
   </formset>
</form-validation>

When creating a form in JSP, you need to place a form element (field) with the property "text" or whatever you defined the validation for.

<html:text property="text" size="16"/>

Also, code for showing validation error messages needs to be placed somewhere on the JSP as well. For example, this code will show error messages.

<logic:messagesPresent>
   <h3><font color="red">
      <bean:message key="errors.header"/>
   </font></h3>
   <ul>
      <html:messages id="error">
         <li><bean:write name="error"/></li>
      </html:messages>
   </ul>
   <p/>
</logic:messagesPresent>

Struts is smart enough to show errors and re-populate all fields from the response object in case the user was redirected to the same page after entering invalid data.

Conclusion

In this article, I described some of the major features of Struts, including its structure, main configuration files, tags, resource files, and internalization support. I also briefly showed its plug-in support, such as validation. I believe that the Struts framework is general enough to be useful in any enterprise Web-based project, yet granular enough to provide a lot of control over the application. Not only can it simplify development process and save work, but it also can provide a layer of abstraction that will take care of the "plumbing" and let developers concentrate on implementation of the business requirements. If you are looking for a Web development framework, you should definitely consider Struts.

Web Development Framework References

Struts: http://struts.apache.org/, http://www.springframework.org/

Hibernate: http://www.hibernate.org/

WebWork: http://www.opensymphony.com/webwork/

Tapestry: http://jakarta.apache.org/tapestry/

About the Author

Vlad Kofman is a Senior System Architect working on projects under government defense contracts. He has also been involved with enterprise-level projects for major Wall Street firms and the U.S. government. His main interests are object-oriented programming methodologies and design patterns.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel