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.
- Figure 5: The Action and ActionForm classes -
The mapping of the request (remember: <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" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<struts-config>
<!-- ========== Form Bean Definitions ================= -->
<form-beans>
<form-bean name="submitForm"
type="hansen.playground.SubmitForm"/>
</form-beans>
<!-- ========== Action Mapping Definitions ============ -->
<action-mappings>
<action path="/submit"
type="hansen.playground.SubmitAction"
name="submitForm"
input="/submit.jsp"
scope="request">
<forward name="success" path="/submit.jsp"/>
<forward name="failure" path="/submit.jsp"/>
</action>
</action-mappings>
</struts-config> |
- 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.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.