Webwork/Struts Taglib to highlight field level errors

One of the things that I really liked about the WebWork/Struts projects was the way the default them would create the field text for you and change the label to red on a field error. The downfall to using the default them was that I had to make my pages in the WebWorks/Struts table and field layout, so I chose to use the simple theme.

Since I chose to use the simple theme, I had to come up with a new way of making the label of a field change to red for a field error. To do this I decided to create a taglib that I could use in my jsp pages to accomplish this. I figured that I could have the taglib accept a field name and cssClass as string arguments. If the field errors map contains a matching field key, then I would append "Err" to the cssClass variable passed into the tag and the css class name is written to the page output. If a error is not found for the field, then the cssClass value is just written out to the page.

Here is the code I used to do the check:

  public static String cssErrorCheck(String field, String cssClass)
  {
    ActionInvocation av = ActionContext.getContext().getActionInvocation();
    ActionSupport as = null;
    if (av != null){
      as = (ActionSupport)av.getAction();
    }
    
    if ((as != null) && (as.hasFieldErrors()))
    {
      if (as.getFieldErrors().containsKey(field))
      {
        return cssClass + "Err";
      }
    }
    return cssClass;
  }

Here is the css styles that I have setup in a stylesheet that is imported on every page:

.label {font-style:normal;}
.labelErr {font-style:italic; color:red; }

So now on my jsp page all I have to do is call the tag in my jsp page and if the field matches a error in the stack, then the field label is changed to red and the text is now in italic.

    <td align=right class="<wwerr:cssErr cssClass="label" fieldName="pe.firstName" />">
      <span class="required">* </span>First Name:
    </td>

If you would like to download the source code for my taglib, then click here.

Posted on Mar 05, 2008 by Mike Jennings in Java | 4 Comments | Permalink



Comments:

Hi, I tried your code for Webwork/Struts Taglib to highlight field level errors.
The issue I encountered was that in the cssErrorCheck method, the ActionInvocation and ActionSupport were always coming back to be null. Is there something missing?
Tks!

Posted by Bips on December 03, 2008 at 05:28 PM EST #

All of the files have been provided. I had this running in both webworks and struts 2 code without a hitch.

Could you give me some more information about your environment like what application server you are running on, what version of the framework you are using, ect. Also are you using Spring as your IOC?

Posted by Mike Jennings on December 04, 2008 at 02:34 PM EST #

Hi Mike, I'm using Struts-2.0.11.1.
Application Server is JBoss-4.2.2.
Not using Spring.
Please correct me if I'm wrong with the following steps:
* Use CssFieldTag.java
* Use wwerr.tld
* Include the Css styles mentioned
* Do the way you suggested for jsp and also include the taglib in the jsp using it.
Tks!

Posted by Bips on December 05, 2008 at 09:51 AM EST #

This setup looks correct, but I have none of the issues you are having. I am using spring as my IOC. Could you try adding spring support to your Struts application so that Struts uses Spring as the IOC.

Posted by Mike Jennings on December 07, 2008 at 09:01 PM EST #

Post a Comment:
  • HTML Syntax: Allowed