Java Tutorial - Java Script :
Understanding Error Handling with Struts
New users need to register with Old Friends Incorporated before receiving services. Before we look at the registration form, let’s examine the errorhandling facility provided by the Struts framework. First, attempt to log into the site without a UserID or password defined by pressing the Submit button (this logic will eventually consult a security database of some sort and apply to invalid logins as well; however, we are just demonstrating basic Struts functionality for now). Once again, let’s drill down through the details of what the Struts framework is doing to trap this error condition. Following is the pertinent section of index.jsp that submits the form for processing:
<html:form action=”oldfriends”>
.... code skipped here ...
.<html:submit value=” Submit “/>
</html:form>
Our HTML taglib specifies “oldfriends” as an action for this form. This action generates a request for the relative URL oldfriends.do. Because of the servlet mapping in the web.xml file, the Struts ActionServlet handles all requests for URLs ending with “.do”. The Struts ActionServlet uses the action mappings in the struts-config.xml to determine how to process the request. It finds the mapping for oldfriends (matching the URL pattern oldfriends. do) and processes that action. We show only the relevant parts of the struts-config.xml in the following code to help with the explanation.
<form-beans>
<form-bean name =”oldfriendsForm” type=”OldFriendsForm” />
</form-beans>
<action-mappings>
<action path=”/oldfriends”
type=”OldFriendsAction”
name=”oldfriendsForm”
input=”/index.jsp”>
<forward name =”main” path=”/main.jsp” />
</action>
</action-mappings>
The path attribute in the action mapping, informs the ActionServlet that requests for oldfriends.do will be handled by this action. The name attribute maps oldfriendsForm to the OldFriendsForm class listed as a form bean. This mapping provides flexibility when a single ActionForm class is used for handling multiple forms. The servlet creates an instance of the Old- FriendsForm class and populates it with the data from the browser provided in the request.
This population is accomplished by matching the property names of the form fields with corresponding get and set method names on the
ActionForm class. The relevant tags from index.jsp are as follows:
<html:text property=”userID” />
<html:password property=”password” />
These names match the setUserID() and the setPassword() methods of the OldFriendsForm class. If the validate attribute is set for the action (it is by default), the servlet then attempts to validate the form by calling the validate() method of the form class. If the validation succeeds, then the servlet calls the execute method of the class defined in the type attribute of the action mapping. If the validation fails, the servlet forwards the request to the page defined by the input attribute and the action class never sees it. The validate() method of the OldFriendsForm class is shown below for convenience.
public ActionErrors validate(
ActionMapping aMapping,
HttpServletRequest aR)
{
ActionErrors err = new ActionErrors();
if(( mUserID == null ) || ( mUserID.length() < 1 ))
err.add(“userid”, new ActionError(“error.userid”));
if(( mPassword == null ) || (mPassword.length() < 1))
err.add(“password”, new ActionError(“error.password”));
return err;
}
You can see that the validation criteria we established earlier are tested against the data that came over from our request. Because we did not enter values for UserID and password, they both evaluate to null. The error messages error.userid and error.password are retrieved from the properties file we discussed earlier and then added to a list of errors before being returned to the controller. A common error for new users is to omit the errors.header and errors.footer definitions in the properties. This results in <null> being displayed for the header when the <html:errors/> tag processes the error map. The relevant portion of ApplicationResources.properties is 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>
