logo
header art

Building an Enterprise-Ready Web Application with Struts

June 23, 2005

Configuring Struts

Now that the webapp is set up to use Struts, the next step is to configure Struts. To do that, we'll flesh out our struts-config.xml file. This file configures Struts with info about the location of message bundles, actions, forwards, and form beans, among other things. Since our webapp isn't using the Tiles or Validator plugins, we'll be removing references to those from the file, as well as the <global-exceptions> and <controller> sections.

  • <struts-config> - this is the root element of the struts-config.xml document.
    • <form-beans> - this element contains a list of <form-bean> tags, each of which describes a single org.apache.struts.action.ActionFormBean.
      • <form-bean> - This particular <form-bean> element describes an ActionFormBean named ShowRecipeForm, which is defined by the com.skillfusion.examples.struts.forms.ShowRecipeForm class.
    • <global-forwards> - this element contains a list of all ActionForwards that should be accessible by name from anywhere in the webapp.
      • <forward> - The only global forward defined for this application is a forward named welcome, which points to the FindRecipe.do URL.
    • <action-mappings> - this element contains a list of all the org.apache.struts.action.Actions defined for this webapp.
      • <action> - The first Action in the list is named ShowRecipe and is defined in the com.skillfusion.examples.struts.actions.ShowRecipeAction. It accepts input from the FindRecipe.jsp, references the ShowRecipeForm defined in the <form-beans> section, uses the request as the default scope, and should be validated using the .validate() method on the aforementioned ActionFormBean.
        • <forward> - This forward uses the built-in name success to tell the Action where it should redirect the user if it succeeds.
        • <forward> - This forward uses the built-in name failure to tell the Action where it should redirect the user if it fails.
      • <action> - This Action is named FindRecipe, and it is much more simple than ShowRecipe. It references the org.apache.struts.actions.ForwardAction built into the Struts framework to redirect to FindRecipe.jsp, the page provided in the parameter attribute.

The next four elements in the file are all <message-resources> elements that tell the webapp where to find the messages that will be displayed to the user throughout the webapp. Using message resources helps maintain the strict separation between the View and Control demanded by the Model 2 architecture and makes it easy to customize the presentation of the webapp for things like internationalization or accessibility. The first <message-resources> element defines the default resource bundle to use for messages in the webapp; it contains messages that don't fit nicely into one of the other categories and can be accessed without specifying a particular resource. The second <message-resources> defines labels for input form fields. The third <message-resources> defines the text that should appear on various buttons throughout the system, and the fourth <message-resources> contains info about the images used in the application, e.g., the alternative text for a given image and the path to it. All the secondary <message-resources> have key attributes that must be used when requesting a message from them. The null that is set to false is an aid to debugging - when it is set to false, a placeholder string appears in the browser window when something in the webapp requests a non-existent message. When this attribute is set to true, a nonexistent message comes back as an empty string.