New Struts 2 Plugin For JSR 303 Bean Validation
Introduction
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:
<dependency>
<groupId>com.github.umeshawasthi</groupId>
<artifactId>struts2-jsr303-validation-plugin</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
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
{
@NotBlank(message="firstName.required")
private String firstName;
@NotBlank(message="lastName.required")
private String lastName;
private String sport;
private String gender;
private String residency;
private boolean over21;
@NotEmpty(message="carModels.required")
private String [] carModels;
@Size(min=1, message="email.required")
@Email(message="email.format")
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:
@Valid
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 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.
References
- 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/
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 :)
you can even mention https://github.com/strutsathon/struts2-jsr303-exam..., which includes few examples as how to use bean validation.
Application also includes example showing how XML based Bean validation can be used
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.
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