WebWork (WW) gives you the ability to easily handle errors within your web application. If you extend your actions from ActionSupport, then you get built in support for error handling. This base class allows you to add generic error message or a specific message associated with an HTML form control. In addition, if you use WW's custom JSP tags, WW will manage form control error display for you.

Lets first look at how to create errors utilizing the base class. In most actions, you have attributes that have setters. These setters are called by WW upon loading the action and setting them with request parameters that were posted by a form. To validate your attribute, you have a couple of options to choose from. You can validate a parameter when it is being set or validate it when doValidate() method is called. Validating a parameter in its setter method makes sense when the validation doesn't require any other information other than the input argument. Otherwise, you should validate within the doValidate().

To validate attributes in a setter you can use a property editor or check the value directly in the method. We will not discuss property editors here see the section called “Validation” for more details on property editors. You have two ways to add error messages from within setters. You can call addError() or throw IllegalArgumentException. The advantage of using IllegalArugmentException is that you are not coupled with the HTML form control name.

addError() example:

* "user" is the HTML form control name

public void setUser(String user) {
   if (user.trim().equals("")) {
      addError("user", "missing user name");
   } else {
      this.user = user;
   }
}

IllegalArgumentException() example:

public void setUser(String user) {
   if (user.trim().equals("")) {
      throw new IllegalArgumentException("missing user name");
   } else {
      this.user = user;
   }
}

Now lets look at validating with doValidate(). Validating here allows you to check across attributes. In this example, we use the addErrorMessage() which does not require us to input the HTML form control name.

doValidate() example:

protected void doValidation()
{
  // Validate
  if (user.length() < 5 && password.length() < 5)
	 addErrorMessage("user and password must be both greater than 5.");
}

Once you have added your errors, handling them is easy. Lets suppose that you have just executed an action which returned ERROR that mapped back to the original form. If you are using WW's custom JSP tags, then you have error handling built in. Any errors added by IllegalArgumentException or addError() will map to the appropriate HTML form control name. To show you how this is done, look at a code snippet from the CSS controlheader.jsp that is included on all UI CSS controls. The normal standard UI controls using tables has error handling built in as well.

Displaying an error for a UI control example:

* This will display the appropriate error message assigned to this control.

<webwork:property value="errors[parameters['name']]">
<webwork:if test=".">
    <div class="errorMessage"><webwork:property value="."/></div>
    </webwork:if>
</webwork:property>

If you added errors with addErrorMessage(), then you can setup your page to display these error messages. For instance, you could add the code snippet below to output all error messages added to your action.

Displaying all error messages example:

* This will printout all error message
<webwork:if test="hasErrorMessages == true">
	ERROR:<BR>
	<webwork:iterator value="errorMessages">
  <FONT COLOR=red><webwork:property/></FONT><BR>
  </webwork:iterator>
</webwork:if>