Step 4: The struts-config.xml file
Up to now it has been simple and straightforward, right? Now I\'ll need to explain a few things. First I\'ll tell you what services the servlet offers to us, and then we\'ll see how we give it the instructions for a specific request. If you look at figure 1--the MVC architecture--you\'ll notice the arrows marked 2 and 3. This is the interaction between the servlet controller and the model, including the business logic. When you use Struts then part of this interaction is as follows:
- First of all the Struts servlet will automatically transfer the data from your form into a JavaBean that you should supply. This bean is called the ActionForm bean, because your bean must extend the Struts ActionForm class. You may think of this bean as a buffer between the browser and your database. The ActionForm bean may also be used to initialize form controls and to validate the data entered by the user.
- Secondly the Struts servlet will call a class which you specify and it is referred to as the Action class. This class may use the data in the ActionForm bean. The Action class is where your application coding starts. When your class finishes it returns control to the Struts servlet.
some-name>.do) to a specific Action and ActionForm class is done in the struts-config.xml file. I have edited the file from struts-blank to suit our one-page application:
<?xml version=1.0 encoding=ISO-8859-1 ?> |
- Figure 6: The struts-config.xml file -
As you can see the file contains two sections: the form-beans section, that lists the ActionForm beans, and the action-mappings.
In the form-beans section you give the bean a logical name (referred to in the action-mapping) and specify the path for the class file.
The action-mappings are the most interesting. The attributes given are these:
path - name of the request: submit.do. You don\'t enter the .do-part here.
type - the path for the Action class file
name - is the logical name of the form bean (from the form-bean section)
input - validation errors should be shown on this page
scope - specifies how long the form bean should live. You may specify session instead.
The forward tag tells the servlet where to go if it receives either success or failure from the Action class. We\'ll return to this feature. In our simple case we always return to the same page.
It\'s wise to standardize on class names. I\'ve used these simple conventions:
Class |
Actual name |
|
ActionForm |
<action>Form, where <action> is the action-path-name |
|
Action |
<action>Action |
We\'re now ready to code the ActionForm and the Action classes--and the jsp view.



