Tuesday, April 7, 2009

Anatomy of ActionForm

The central class for performing simple validations is
org.apache.struts.action.ActionForm

Struts requires you to associate each form displayed to the user with your subclass of ActionForm.

Your subclass of ActionForm does two things:
  • It holds all form data: Your subclass of ActionForm must have getters and setters corresponding to each property of the form.
  • It performs simple validation: Your subclass must override the validate() function, if you want to perform any simple checks.

When a user submits a form, Struts populates the associated ActionForm subclass with the data, then calls validate() to perform any simple validations required. This function looks like this:
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)

ActionErrors, which is validate()’s return value, is essentially like a HashMap to store error messages by key.

  • If validate() returns a null value (or an empty ActionErrors instance), the validation passes.
  • If validate() returns a non-empty ActionErrors instance, the validation fails, and Struts redisplays the form to the user along with any error messages.


No comments:

Post a Comment

Followers