The Bean Validation plugin implements a bridge to use Bean Validation in struts actions. Bean Validation has been specified in JSR 303 and is part of the JavaEE platform. Full-fledged application servers already bring validation providers which can be leveraged by this plugin. The plugin integrates with other struts features like:
- model driven
- AJAX Validation
In order to use the Bean Validation plugin, you first need to add the JAR file to the
WEB-INF/lib directory of your application or include the dependency in your project's Maven POM file.
Where X.X.X is the current version of Struts 2. Please remember that the Bean Validation Plugin is available from version 2.5.
This sample shows the configuration constants the plugin provides. It also shows how to enable bean-validation by extending your own application package from
struts-bean-validation which comes along with the plugin.
Here is another example that shows how you can combine bean-validation with other plugins by configuring your own Interceptor-Stack (note: this is just a very short example. In a real app you should take more care about your stack). You can combine bean validation with classic struts validation (or disable either) by putting the according interceptors in your stack (or by removing them from your stack).
Bean Validation Example
Here is an example Action that makes use of bean validation. Note that some of the validation annotations are taken from
javax package (which is defined in the JSR) while others are taken from the validaton provider (in this case:
hibernate). You can specifiy own text keys in the
message attribute of the annotations. If you do that the whole struts i18n mechanism kicks in to resolve those text keys.