- Hello World! Application Requirements
- Applying the Model-View-Controller Pattern
- The View Component: The HTML Form and the Form Bean
- MessageResources and Application.properties Files
- The Struts Form Bean: HelloForm.java
- Data Validation and ActionErrors
- The Controller Component: HelloAction.java
- The Model Component (HelloModel.java)
- Passing Data to the View Using Attributes: Constants.java
- Tying It All Together: The struts-config.xml File
- Conclusions
Data Validation and ActionErrors
Creating data validation logic is an important and time-consuming part of any application. Fortunately, Struts provides an easy-to-use, powerful way of handling this for you.
This functionality is broken into two pieces:
Providing an easy-to-use method of capturing error information as it is discovered
Making that information available to the View component in a manner that makes it easy for the View component to access and display the information as needed
The Struts framework provides two classes to assist you with this:
ActionErrorThis class is used to represent a single validation error.
ActionErrorsThis class provides a place to store all the individual ActionError objects you create. As you create ActionError objects, you simply stuff them into the ActionErrors holder and continue processing.
These two classes solve the problem: The ActionError classes make it easy to capture errors when it's convenient in your code. The ActionErrors class stores them and makes them easily available to your JSP files.
The Hello World! application has been defined to have two different types of errors: basic data/form validation errors and business logic errors. The requirements for the two types are
Form validationIn the data entry form, make sure that the user doesn't submit the form with the person field empty.
Business logicEnforce a rule that the user can't say hello to a person he isn't allowed to talk to. (Because Atilla the Hun has such a bad reputation, let's make him the person we won't speak to.)
NOTE
The reason the errors are broken into these two types is purely for demonstration purposes. It enables us to show error handling in both the form bean and in the Action class (which will be introduced in the next section).
In practice, it's not uncommon to break validation into these two categories. Do whatever makes sense for your application.
The first type of validation (data/form validation) is done in the form bean. This is demonstrated by the following code segment from HelloForm.java:
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if ((person == null) || (person.length() < 1)) errors.add("person", new ActionError("ch01.hello.no.person.error")); return errors; }
If the validate() method returns the ActionErrors object empty, Struts assumes there are no errors and processing moves to the Action class. If ActionErrors contains any ActionError elements, the user is redirected to the appropriate page to correct the errors.
If processing is redirected for the user to correct the data entry, the ActionErrors object carries the individual ActionError elements back to the View for display. The View component can access the ActionErrors either directly or through the <html:errors> tag.
Validation can be enabled or disabled on a page-by-page basis through settings in the Struts configuration file (struts-config.xml).