Tweak two XML files to easily configure your Struts application

The Struts Framework can help abstract your back-end code from the display layer and keep your code reusable. See how to easily configure your Struts app by modifying two key XML files, Web.xml and Struts-config.xml.

The Struts Framework offers a powerful Java-based tool that helps you separate your back-end data from your application’s presentation layer. Let's discuss how to further take advantage of Struts by easily configuring it with some XML modifications. The two key configuration files you'll need to adjust are Web.xml and Struts-config.xml.

Start me up
The application server reads the contents of the Web.xml to load and configure your Struts servlet. Listing A shows a typical Web.xml file. The following lists some pertinent information you should know about Web.xml:
  • ·        <Servlet> tag names your application and specifies that it use the Struts Servlet. You can nest any number of parameters within the <Servlet> tag.
  • ·        If the application server should load your application at a certain point, specify the relative order in the <load-on-startup>. Lower numbers are loaded first.
  • ·        The <url-pattern> within the <Servlet-mapping> tells the application server what page requests to forward to your application. The mapping *.do is used. The Servlet-name is identical to the one specified in the <Servlet> tag.
  • ·        The welcome page (i.e., the default first page for your application) must be a physical file and is specified within the <welcome-file-list> tags.
  • ·        Struts has a number of standard JSP tab libraries. Specify the ones your application uses in separate <taglib-uri> entries in the <taglib-uri> section.

Putting it all together
A typical Struts application is extremely modular and contains numerous JSP pages and Java Beans. The Struts-config.xml ties these modules together and determines which module to call when and in what order. A key function of this configuration file is to map logical names to physical paths. The physical paths are relative to the application’s path.

Although the database layer is not integral to the Struts model, you can specify database information. Listing B shows a <data-source> for MySQL. Refer to your database driver documentation to determine how to specify your datasource.

Each ActionForm in your application will have a corresponding Java Bean. A <form-bean> entry is required for each Form Java bean in your application. Global forwards can be used throughout your application. Typically, you'd specify elements for global navigation; their logical names are mapped in the <global-forwards> section.

An <action> specifies more than just mapping information. Typically, Struts will validate a bean before providing it to the action. Forwards within an <action> are local to that action and supply the next step to take in case of success or failure. The <action> can specify a page, request, session, or application scope.

Simplifying the configuration
Struts simplifies your application by separating configuration information from your code. Struts-config.xml implements a powerful feature of Struts by mapping logical names to physical file. This mapping allows you to change the application flow and file locations without reprogramming. The Web.xml file is used when the application server starts and loads servlets. The ActionServlet reads the Struts-config.xml file and stores its contents into a database at startup. When you make changes to these configuration files, you may need to restart the Servlet before the changes will take effect.

Editor's Picks