Thursday, 1 October 2009

Basics of a struts config file.

Any struts application has a struts config file which defines the mappings for any request that comes to the controller for processing. Understanding the concepts of this file can help you get a clear picture of Struts.
In this tutorial, we will just look at the normal facilities offered by the strutsconfig.xml.

The Struts configuration file adheres to the struts-config_1_1.dtd. The struts config dtd can be found in the Struts distribution in the lib directory. It shows every possible element, their attributes and their description. Covering all of them at once would only result in information overload. Hence we will only look at the five important sections of this file relevant to our discussion and their important attributes. In fact we have already covered most of these in the lifecycle discussion earlier, but are summarizing them again to refresh your mind.

The five important sections are:
1. Form bean definition section
2. Global forward definition section
3. Action mapping definition section
4. Controller configuration section
5. Application Resources definition section

Listing below shows a sample Struts Config file showing all the five sections. The form bean definition section contains one or more entries for each ActionForm. Each form bean is identified by a unique logical name. The type is the fully qualified class name of the ActionForm. An interesting to note is that you can declare the same ActionForm class any number of times provided each entry has a unique name associated with it. This feature is useful if you want to store multiple forms of the same type in the servlet session.

Table: Important attributes and elements of ActionMapping entry in struts-config.xml

Attribute/Element name Description
Path The URL path (either path mapping or suffix mapping) for which this Action Mapping is used. The path should be unique
Type The fully qualified class name of the Action
Name The logical name of the Form bean. The actual ActionForm associated with this Action Mapping is found by looking in the Form-bean definition section for a form-bean with the matching name. This informs the Struts application which action mappings should use which ActionForms.
Scope Scope of the Form bean – Can be session or request
Validate Can be true or false. When true, the Form bean is validated on submission. If false, the validation is skipped.
Input The physical page (or another ActionMapping) to which control should be forwarded when validation errors exist in the form bean.
Forward The physical page (or another ActionMapping) to which the control should be forwarded when the ActionForward with this name is selected in the execute method of the Action class.

The ActionMapping section contains the mapping from URL path to an Action class (and also associates a Form bean with the path). The type attribute is the fully qualified class name of the associated Action. Each action entry in the action-mappings should have a unique path. This follows from the fact that each URL path needs a unique handler. There is no facility to associate multiple Actions with the same path. The name attribute is the name of the Form bean associated with this Action. The actual form bean is defined in Form bean definition section. Table above shows all the relevant attributes discussed so far for the action entry in action-mappings section.

//Sample struts-config.xml

"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
Form bean Definitions

Global Forward Definitions

Action Mappings

redirect=”true” />
path="/Failure.jsp" />

type=”org.apache.struts.action.ForwardAction” />
Controller Configuration
processorClass="org.apache.struts.action.RequestProcessor" />

Message Resource Definition

In the ActionMapping there are two forwards. Those forwards are local forwards – which means those forwards can be accessed only within the ActionMapping. On the other hand, the forwards defined in the Global Forward section are accessible from any ActionMapping. As you have seen earlier, a forward has a name and a path. The name attribute is the logical name assigned. The path attribute is the resource to which the control is to be forwarded. This resource can be an actual page name as in

or it can be another ActionMapping as in

The /logoff (notice the absence of “.do”) would be another ActionMapping in the struts-config.xml. The forward – either global or local are used in the execute() method of the Action class to forward the control to another physical page or ActionMapping.

The next section in the config file is the controller. The controller is optional. Unless otherwise specified, the default controller is always the org.apache.struts.action.RequestProcessor. There are cases when you want to replace or extend this to have your own specialized processor. For instance, when using Tiles (a JSP page template framework) in conjunction with Struts, you would use TilesRequestProcessor.

The last section of immediate interest is the Message Resource definition. In the ActionErrors discussion, you saw a code snippet that used a cryptic key as the argument for the ActionError. We stated that this key maps to a value in a properties file. Well, we declare that properties file in the struts-config.xml in the Message Resources definition section. The declaration in Listing above states that the Message Resources Bundle for the application is called and the file is located in the java package mybank.

If you are wondering how (and why) can a properties file be located in a java package, recall that any file (including class file) is a resource and is loaded by the class loader by specifying the package.

No comments:

Post a Comment