JSR-303 standardizes validation constraint declaration and metadata for the Java platform. Using this API, you annotate domain model properties with declarative validation constraints and the runtime enforces them. There are a number of built-in constraints you can take advantage of. You may also define your own custom constraints.
To illustrate, consider a simple PersonForm model with two properties:
public class PersonForm {
private String name;
private int age;
}
JSR-303 allows you to define declarative validation constraints against such properties.
public class PersonForm {
@NotNull
@Size(max=64)
private String name;
@Min(0)
private int age;
}
When an instance of this class is validated by a JSR-303 Validator, these constraints will be enforced.
A JSR-303 Bean Validation Plguin for Struts2. This Plugin works as a bridge between Struts2 request flow and JSR-303 Compliant bean Validator like Hibernate Validator.
This plugin itself do not provide JSR-303 Specific validationn but will use underlying validator to perform bean validation.
For general information on JSR-303, see the Bean Validation Specification. For information on the specific capabilities of the default reference implementation, see the Hibernate Validator documentation.
<script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-12895099-2', 'github.com'); ga('send', 'pageview'); </script>This tutorial will give you introduction as how to use Struts2-jsr303 validation plugin.I am assuming that person has basic understanding about bean Validation API and its specifications.
We need to install Struts2 JSR303 bean validation Plugin
.You can either download plugin jar manually from download page and include reference implementation
and validation-api
version 1.0.0.GA from download validation-api 1.0.0
you can also use maven to take care of adding plugin to your Struts2
application.Add following to you pom.xml
<dependency>
<groupId>com.github.umeshawasthi</groupId>
<artifactId>struts2-jsr303-validation-plugin</artifactId>
<version>1.0</version>
</dependency>
Plugin as been tested with following reference implementations
- Hibernate Validator
- Apache BVal
However plugin will work with any reference implementations in accordance with bean validation 1.0 specifications.
In order to use Plugin with struts2, all you need to extends your package with jsr303
and you are all set to use underling features of bean validation API like
<package name="default" extends="jsr303">
<default-interceptor-ref name="jsr303ValidationStack"/>
// action mapping
</package>
Make sure to add Hibernate validator or Apache Bval in your Struts2 application.Plugin does not provide any validation itself but work as a bridge between Struts2 and Bean Validation API
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-validation_1.0_spec</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.bval</groupId>
<artifactId>org.apache.bval.bundle</artifactId>
<version>0.5</version>
</dependency>
Plugin has been tested with 1.0 Specifications and not with 1.1, as Apache Bval 1.1 implementation is still under development.
Plugin utilize Struts2 Message resolving algorithm. You can create action specific files or can create single file for entire application. You can even pass messages directly in the validation annotation.
@Size(min=10, max=10, message="mobile.number") private String mobile;
in above example mobile.number
will work as key and plugin will search messages with given key in underlying resource bundles with help of Struts2.
You can directly provide messages in validation constraints itself
@Size(min=10, max=10, message="Please provide a valid mobile number") private String mobile;
In case of no message, plugin will fall back to default messages provided by Validation API.
Plugin try to work and search in class path about Bean validator provider, but you can always provider your prefered Bean Validator by setting
struts.jsr303.beanValidator.providerClass
in struts.xml file or in property file
<struts>
............................
<constant name="struts.jsr303.beanValidator.providerClass" value="org.hibernate.validator.HibernateValidator"/>
..............
</struts2>
Please note that setting struts.jsr303.beanValidator.providerClass
constant is not required,however in case you have multiple provider in your
classpath and want your prefreed bean validator, you can always set it by struts.jsr303.beanValidator.providerClass
constant in
your struts.xml file.
For your convience, here is the list of provider.
`org.hibernate.validator.HibernateValidator` `org.apache.bval.jsr303.ApacheValidationProvider`You can always look inside META-INF/services/javax.validation.spi.ValidationProvider
.