Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

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:

  • ActionError—This class is used to represent a single validation error.

  • ActionErrors—This 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 validation—In the data entry form, make sure that the user doesn't submit the form with the person field empty.

  • Business logic—Enforce 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.)


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).

  • + Share This
  • 🔖 Save To Your Account