Building an Enterprise-Ready Web Application with Struts
June 23, 2005
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 singleorg.apache.struts.action.ActionFormBean.<form-bean>- This particular<form-bean>element describes anActionFormBeannamedShowRecipeForm, which is defined by thecom.skillfusion.examples.struts.forms.ShowRecipeFormclass.
-
<global-forwards>- this element contains a list of allActionForwards that should be accessible by name from anywhere in the webapp.<forward>- The only global forward defined for this application is a forward namedwelcome, which points to the FindRecipe.do URL.
-
<action-mappings>- this element contains a list of all theorg.apache.struts.action.Actions defined for this webapp.-
<action>- The firstActionin the list is namedShowRecipeand is defined in thecom.skillfusion.examples.struts.actions.ShowRecipeAction. It accepts input from the FindRecipe.jsp, references theShowRecipeFormdefined in the<form-beans>section, uses the request as the default scope, and should be validated using the.validate()method on the aforementionedActionFormBean.<forward>- This forward uses the built-in namesuccessto tell theActionwhere it should redirect the user if it succeeds.<forward>- This forward uses the built-in namefailureto tell theActionwhere it should redirect the user if it fails.
-
<action>- ThisActionis namedFindRecipe, and it is much more simple thanShowRecipe. It references theorg.apache.struts.actions.ForwardActionbuilt into the Struts framework to redirect to FindRecipe.jsp, the page provided in theparameterattribute.
-
-
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.

