Java Tutorial - Java Script : Building a Sample Application with Struts

Java Tutorial - Java Script :

Building a Sample Application with Struts


We have completed the construction of the major pieces of our initial screen for the Old Friends Incorporated application; we now need to take a look at how we glue them together. In other words, how does the Struts controller know which Action to invoke for a given request? This gluing (or mapping) of the actions is defined in the file struts-config.xml that can be found in the WEB-INF directory of the Web application. Here is a look at the complete struts-config.xml file:

<?xml version=”1.0” encoding=”ISO-8859-1” ?>
<!DOCTYPE struts-config PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 1.1//EN” “http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd”>
<struts-config>
<!-- Form Bean Definitions ================================ -->
<form-beans>
<form-bean name =”oldfriendsForm” type=”OldFriendsForm” />
<form-bean name =”emptyForm” type=”EmptyForm” />
<form-bean name =”registerForm” type=”RegisterForm” />
</form-beans>
<!-- Global Forward Definitions =========================== -->
<global-forwards>
</global-forwards>
<!-- Action Mapping Definitions ============================-->
<action-mappings>
<action path=”/about”
type=”EmptyAction”
name=”emptyForm”>
<forward name=”Success” path=”/about.jsp”/>
</action>
<action path=”/mainscreen”
type=”EmptyAction”
name=”emptyForm”
validate=”false”
input=”/register.jsp”>
<forward name =”Success” path=”/index.jsp” />
</action>
<action path=”/oldfriends”
type=”OldFriendsAction”
name=”oldfriendsForm”
input=”/index.jsp”>
<forward name =”main” path=”/main.jsp” />
</action>
<action path=”/startregister”
type=”EmptyAction”
name=”emptyForm”
validate=”false”
input=”/index.jsp”>
<forward name =”Success” path=”/register.jsp” />
</action>
<action path=”/registerform”
type=”RegisterAction”
name=”registerForm”
validate=”true”
input=”/register.jsp”>
<forward name =”main” path=”/main.jsp” />
</action>
</action-mappings>
<message-resources parameter=”ApplicationResources”/>
</struts-config>
Near the beginning of our configuration file, you will notice the section for our Form bean classes, which we list by itself here:
<!-- Form Bean Definitions ================================ -->
<form-beans>
<form-bean name =”oldfriendsForm” type=”OldFriendsForm” />
<form-bean name =”emptyForm” type=”EmptyForm” />
<form-bean name =”registerForm” type=”RegisterForm” />
</form-beans>
This snippet of XML describes the bean names for our action mappings and the accompanying class that contains the Form bean. The other significant part of the configuration is the action mappings, an example of which follows:
<action path=”/oldfriends”
type=”OldFriendsAction”
name=”oldfriendsForm”
input=”/index.jsp”>
<forward name =”main” path=”/main.jsp” />
</action>

The Struts servlet uses the action mappings to determine how to handle a request. In the example above, the Struts servlet will handle a request for the URL ending with oldfriends.do by:

·         Creating an instance of the OldFriendsForm class
·         Populating it with the fields from the input.jsp page
·         Sending it to the OldFriendsAction form

The OldFriendsAction form can then send the application on to main.jsp or back to index.jsp, depending on how it handles the content on the form. There are a couple of other configuration files that we need to look at before launching our application. The web.xml file that was provided by the Struts- Blank project that we started with should be okay with no changes. Next, we create the ApplicationResources.properties file in the classes directory.

This file was specified in the following line that appears in the struts-cfg.xml file:
<message-resources parameter=”ApplicationResources”/>
The file is a standard Java resource bundle and provides the message text that will be used in our application. The contents are shown below:
error.userid=<li>UserID is required.</li>
error.password=<li>Password is required.</li>
errors.header=<h3>Errors:</h3><ul>
errors.footer=</ul>
oldfriends.highschool=Mullins High School
error.register.useridpasswd=<li>UserId and Password are required.</li>
error.register.userfname=<li>First Name is required.</li>
error.register.userlname=<li>Last Name is required.</li>
errors.register.header=<h3>Errors:</h3><ul>
errors.register.footer=</ul>