Home

This Chapter
-Chapter 2: Input Validation with Action Forms
-An Overview of Action Forms
-The ActionForm Class
-Using Action Forms
-Using HTML Tag Libraries
-Another Example
-Multipage Forms
-Dynamic Action Forms
-Summary

Table of Contents
-Introduction
-Chapter 1: Model 2 and Struts
-Chapter 2: Input Validation with Action Forms
-Chapter 3: The HTML Tag Library
-Chapter 4: Input Validation and Data Conversion
-Chapter 5: The Validator Plugin
-Chapter 6: The Expression Language
-Chapter 7: JSTL
-Chapter 8: The Bean Tag Library
-Chapter 9: The Logic Tag Library
-Chapter 10: Struts-EL, Nested, selectLabel
-Chapter 11: Message Handling and Internationalization
-Chapter 12: The Tiles Framework
-Chapter 13: Securing Struts Applications
-Chapter 14: The Config Object
-Chapter 15: The Persistence Layer
-Chapter 16: Object Caching
-Chapter 17: File Upload and File Download
-Chapter 18: Paging and Sorting
-Chapter 19: Preventing Double Submits
-Chapter 20: Early HttpSession Invalidation
-Chapter 21: Decorating Request Objects
-Chapter 22: How Struts Works

Previous
Next

 

The ActionForm Class

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.

  1. If the name attribute of the action element is present, create an instance of the ActionForm. The class name can be retrieved from the corresponding form-bean element.
  2. Store the ActionForm instance in either the HttpServletRequest object or the HttpSession object, depending on the value of the scope attribute of the action element. If the scope attribute is missing, the instance will be stored as an attribute of the HttpServletRequest object.

    Figure 2.1: How the action servlet processes an action form

  3. Populate the form from the HttpServletRequest object’s parameters by calling the getParameterValues method on the HttpServletRequest object.
  4. Unless the validate attribute of the action element equals false, validate the form by calling the form’s validate method. If the method returns a non-empty ActionErrors object, store the ActionErrors object as an attribute of the HttpServletRequest object.
  5. If validation was successful, call the execute method on the associated action object, if any, and forward to the view. If validation failed, return control to the resource specified in the input attribute of the action element.

Previous
Next