> Home

Struts Error Highlighting

Holiday Accommodation in Croatia

Struts now has the ability to automatically highlight error fields.

How it Works

Struts input tags (e.g. <html:text>) have always had the style, styleId and styleClass attributes that render HTML style, id and class elements respectively and can be used with CSS to control the look and feel of form elements. Now three equivalent error attributes have also been added which are errorStyle, errorStyleId and errorStyleClass. The way these work is that if an error is found these new error attributes override the normal attributes (if specified) when rendering the HTML style, id and class elements.

I guess your asking "How does the Tag know when theres an error?" - well the tags check for an error message associated with the form field. For this to work the message(s) need to conform to the following criteria:

Example A - Basic Example

If you use, for example, the styleClass attribute to control how the input fields on your form look, then you simply need to add the errorStyleClass attribute to the tags and struts will use the value specified in errorStyleClass when there are errors for that property and the value in styleClass when there are no errors to render the class element.

Your Struts form would look something like this...

    
      <html:errors/>
      
      <html:form action="myAction">

           <html:text property="custNo"      styleClass="fieldCenter" errorStyleClass="fieldCenterError"/>
           <html:text property="custName"    styleClass="fieldLeft"   errorStyleClass="fieldLeftError"/>
           <html:text property="custAddress" styleClass="fieldLeft"   errorStyleClass="fieldLeftError"/>

      </html:form>

Using the ActionForm's validate method...

    
      public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 

          ActionErrors errors = new ActionErrors();

          if (custNo == null) {
              errors.add("custNo", new ActionMessage("custNo.missing"));
          }
          if (custName == null) {
              errors.add("custName", new ActionMessage("custName.missing"));
          }
          if (custAddress == null) {
              errors.add("custAddress", new ActionMessage("custAddress.missing"));
          }

          return errors;
 
      }

N.B. The above example uses the errorStyleClass - remember to set up any new error style classes in your CSS Style Sheet.

Example B - Using an alternative error key

Maybe you want to do your validation in your Action's execute method instead and store the errors under an alternative key. Then you would need to do something like the following

    public ActionForward execute(ActionMapping mapping, 
                                 ActionForm form, 
                                 HttpServletRequest request, 
                                 HttpServletResponse response) throws Exception { 

          ActionMessages errors = new ActionMessages();

          if (custNo == null) {
              errors.add("custNo", new ActionMessage("custNo.missing"));
          }
          if (custName == null) {
              errors.add("custName", new ActionMessage("custName.missing"));
          }
          if (custAddress == null) {
              errors.add("custAddress", new ActionMessage("custAddress.missing"));
          }

          // store the errors under a custom key
          if (errors.size() > 0) {
              request.setAttribute("myErrorKey", errors);
              return mapping.getInputForward();
          }

          return mapping.findForward("...");
        
    }

On your Struts form you need to use the key your ActionMessages are stored under for displaying the messages (in this example specifying the error attribute on the <html:errors> tag) and on the jsp tag for the form fields (in this example using the errorKey attribute on the <html:text> tag).

    
      <html:errors error="myErrorKey"/>
      
      <html:form action="myAction">

           <html:text property="custNo"      errorKey="myErrorKey"  errorStyle="background-color: red"/>
           <html:text property="custName"    errorKey="myErrorKey"  errorStyle="background-color: red"/>
           <html:text property="custAddress" errorKey="myErrorKey"  errorStyle="background-color: red"/>

      </html:form>

N.B. The above example uses the errorStyle attribute which renders a style attribute rather than the errorStyleClass attribute which renders a class attribute.

BeanUtils Problem Affecting Error Styles

January 28th 2005 - a problem in Commons BeanUtils has been discovered which causes error hightlighting not to work in some circumstances for indexed properties. Details of the issue can be found in Struts Bug 33258 and BeanUtils Bug 28358.

Suggested Customisation

Personally, I can't be bothered with keying in all these additional error styles on my forms especially since its a behaviour I want on every form. I suggest creating custom versions of the Struts input tags which automatically generate the error style class by adding a suffix (e.g. Error) to the styleClass. For example, a custom TextTag would look like the following...


import org.apache.struts.taglib.html.TextTag;

public class TextErrorTag extends TextTag {

    public String getErrorStyleClass() {

        return (getStyleClass() == null) ? null : getStyleClass() + "Error";

    }

}

NOTE: You will either have to modify the Struts HTML tags TLD to use your new tag(s), or set up your own custom TLD.