The org.apache.struts.action.ActionForm class is a JavaBean associated with an HTML form. Each property in an action form represents a field in the HTML form. The property name must be the same as the associated field.
You override the reset method of ActionForm to initialize the form’s property values. Note that the reset method cannot be used to reset the values of the associated HTML form.
You write validation rules by overriding the validate method of ActionForm. Here is the signature of the validate method:
public ActionErrors validate(ActionMapping mapping, javax.servlet.ServletRequest request)
where ActionErrors is a collection that can contain ActionError objects (more about this in Chapter 3).
Validation is successful if the validate method returns null or an empty ActionErrors object. If it returns a non-empty ActionErrors, the validation failed.
For example, if you have defined that to add a product, both its productName and description properties must not be empty, you can write the following validation rules:
public ActionErrors validate(ActionMapping mapping,
javax.servlet.ServletRequest request) {
ActionErrors actionErrors = new ActionErrors();
If (productName.equals("")) {
ActionErrors.add(...);
}
If (description.equals("")) {
ActionErrors.add(...);
}
return actionErrors;
}
If validation was successful, control will be forwarded to the action object of that action. If validation failed, control is forwarded to a resource specified in the input attribute of the given action element. If no input attribute is present, the user will see an error message.
The exact description of how the action servlet processes an action form is given in Figure 2.1.
When working with action forms, you use the following attributes in the corresponding action element: name, input, scope, validate. The Struts action servlet does the following when an action is invoked.
Figure 2.1: How the action servlet processes an action form