ponder275 ponder275 - 1 year ago 116
HTML Question

How to highlight fields which fail Struts2 validation?

I have a user who would like to have the fields which fail Struts2 validation highlighted.

I plan to set a flag for any field which fails in my

method and use that field to change the background of the field when my JSP page is loaded.

My question is how do I handle the fields which fail the validation in my
file ? Do I need to do all of my validation in my
method ?

Also is there a way to highlight the fields which generate an

Invalid field value for field ...

error ?

Any help would be appreciated!


This feature is already built in Struts2, no need for custom flags or whatever. In Struts2 form tags there is cssErrorClass attribute which will set CSS class for field on error.

<style type="text/css">
    .errField {
        background-color: red;

<s:textfield key="name" cssErrorClass="errField" />