New Struts 2 Plugin For JSR 303 Bean Validation


A talented Struts 2 developer, Umesh Awasthi, has created a new Struts 2 plugin to provide Bean (JSR 303) validation as part of the Struts 2 framework.

This article provides notes and an example application you may review to learn how to use the Struts 2 JSR 303 validator plugin. Generally, including the plugin and a JSR 303 compliant implementation in your project will cause the Struts 2 framework to apply the validation constraints you have included on your Bean classes. Note that the 1.0 version of the plugin does not allow you to combine both JSR 303 bean validation with the Struts 2 XML validation.

Struts 2 JSR 303 Validator Plugin

The plugin's source code is in GitHub at If you are using Maven you can add a dependency to your pom.xml file:



Also note that you will need to include a JSR 303 compliant implementation so that the Struts 2 JSR 303 validator plugin will know which validation library to use. The second dependency node above adds the JSR 303 reference implementation.

Modify Struts Configuration

To tell your Struts 2 project to use the plugin you must modify the Struts XML configuration so that your package uses the jsr303 set of Struts 2 interceptors. For example:

    <package name="default" extends="jsr303">
    <default-interceptor-ref name="jsr303ValidationStack"/>

Apply Validation Annotations to Bean Class

If you are not familiar with how to use the JSR 303 Bean validation annotations consult the references below. In the example application I used several different annotations to enforce business rules such as the first and last name cannot be empty, the email address must be correctly formatted, the phone number must be in a specific format, and the user must select at least one checkbox in the list of car models.

public class Person
private String firstName;
private String lastName;

private String sport;
private String gender;
private String residency;
private boolean over21;

private String [] carModels;

@Size(min=1, message="email.required")
private String email;

@Pattern(regexp="\\d{3}-\\d{3}-\\d{4}", message="phoneNumber.required")
private String phoneNumber;

The message values refer to keys found in a properties file. If the validation fails the value of the key from the property file will be displayed. This allows you to use property files for various locales so that the error messages will display in the appropriate language.

Enforce Validation In the Action Class

In the action class that will process the form submission you must use the Valid annotation on the object that will receive the values entered in the form submission fields. For example:

    private Person personBean;

If any of the fields in the Person class fail validation then the Struts 2 framework will return "input" which in the example application results in the JSP with the form being redisplayed to the user. The form fields that caused validation to fail will have error messages (the values of the message keys) displayed just above them.

Example Application

You can checkout the example application as part of the Struts 2 tutorial examples at Those examples are stored in a Subversion repository that is open for read access. This example is named bean_validation and includes a ReadMe.txt for how to build and run the project.


  1. jsr303-validator-plugin source code and ReadMe,
  2. Struts 2 Plugin Announcement
  3. JSR 303 Bean Validation Specification,
  4. JSR 303 Reference Implementation, Hibernate Validator,

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Thanks Bruce for blog post.
Everything seems to be good.

We are missing one additional information as bean validation can be enforced using xml also.

I will update plugin page for this soon :)
# Posted By Umesh Awasthi | 10/29/13 9:37 AM

you can even mention, which includes few examples as how to use bean validation.

Application also includes example showing how XML based Bean validation can be used
# Posted By Umesh Awasthi | 10/29/13 9:43 AM

I use struts2-fullhibernatecore-plugin in order to be able to inject hibernate session and transaction.

I tried a lot to do and nothing helps.

struts2-jsr303-validation-plugin doesn't show errors on jsp page.
But it prevents hibernate to insert into database new object.

Please, try to use this plugin and fix the issues.
I am sure it won't work for you too.
# Posted By Volodia | 2/3/14 1:16 PM
Hi Volodia,

Can you put sample application on github so as I can see what issue you are facing?
Ideally struts2-jsr303 plugin should show error messages on the UI /JSP
but need more information to debug or fix issue in plugin
# Posted By Umesh | 3/4/14 11:03 PM
BlogCFC was created by Raymond Camden. This blog is running version Contact Blog Owner