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 https://github.com/umeshawasthi/jsr303-validator-plugin. 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">
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;
private String email;
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.
You can checkout the example application as part of the Struts 2 tutorial examples at https://svn.apache.org/repos/asf/struts/sandbox/trunk/struts2examples. 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.
- jsr303-validator-plugin source code and ReadMe, https://github.com/umeshawasthi/jsr303-validator-plugin
- Struts 2 Plugin Announcement
- JSR 303 Bean Validation Specification, http://beanvalidation.org/1.0/spec/
- JSR 303 Reference Implementation, Hibernate Validator, http://docs.jboss.org/hibernate/validator/4.3/reference/en-US/html/