Fork me on GitHub
Edit on GitHub << back to Plugins

Bean Validation Plugin

Bean Validation

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:


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.


    <constant name="struts.beanValidation.providerClass" value="org.hibernate.validator.HibernateValidator"/>
    <constant name="struts.beanValidation.ignoreXMLConfiguration" value="false"/>
    <constant name="struts.beanValidation.convertMessageToUtf" value="false"/>
    <constant name="struts.beanValidation.convertMessageFromEncoding" value="ISO-8859-1"/>

    <package name="my-bean-validation" extends="struts-bean-validation">

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).


<package name="my-app-package" extends="struts-default">
        <interceptor-stack name="appDefaultStack">
            <interceptor-ref name="beanValidation"/>
            <interceptor-ref name="defaultStack"/>

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.


package com.example.actions;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts.beanvalidation.constraints.FieldMatch;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.ScriptAssert;
import org.hibernate.validator.constraints.URL;

import javax.validation.constraints.*;
import java.util.Date;

@Action(results = {
        @Result(name = "input", location = "bean-validation.jsp"),
        @Result(name = "success", location = "/WEB-INF/validation/successFieldValidatorsExample.jsp")
@FieldMatch(first = "fieldExpressionValidatorField", second = "requiredValidatorField", message = "requiredValidatorField and fieldExpressionValidatorField are not matching")
@ScriptAssert(lang = "javascript", script = "_this.dateValidatorField != null && _this.dateValidatorField.before(new java.util.Date())", message = "Date need to before now")
public class BeanValidationExampleAction extends ActionSupport {
    private String requiredValidatorField = null;

    private String requiredStringValidatorField = null;

    @NotNull(message = "")
    private Integer integerValidatorField = null;

    private Date dateValidatorField = null;

    @Size(min = 4, max = 64)

    private String emailValidatorField = null;

    @Size(min = 4, max = 64)
    private String urlValidatorField = null;

    @Size(min = 2, max = 4)
    private String stringLengthValidatorField = null;

    @Pattern(regexp = "[^<>]+")
    private String regexValidatorField = null;

    private String fieldExpressionValidatorField = null;

    public Date getDateValidatorField() {

        return dateValidatorField;

    public void setDateValidatorField(Date dateValidatorField) {
        this.dateValidatorField = dateValidatorField;

    public String getEmailValidatorField() {
        return emailValidatorField;

    public void setEmailValidatorField(String emailValidatorField) {
        this.emailValidatorField = emailValidatorField;

    public Integer getIntegerValidatorField() {
        return integerValidatorField;

    public void setIntegerValidatorField(Integer integerValidatorField) {
        this.integerValidatorField = integerValidatorField;

    public String getRegexValidatorField() {
        return regexValidatorField;

    public void setRegexValidatorField(String regexValidatorField) {
        this.regexValidatorField = regexValidatorField;

    public String getRequiredStringValidatorField() {
        return requiredStringValidatorField;

    public void setRequiredStringValidatorField(String requiredStringValidatorField) {
        this.requiredStringValidatorField = requiredStringValidatorField;

    public String getRequiredValidatorField() {
        return requiredValidatorField;

    public void setRequiredValidatorField(String requiredValidatorField) {
        this.requiredValidatorField = requiredValidatorField;

    public String getStringLengthValidatorField() {
        return stringLengthValidatorField;

    public void setStringLengthValidatorField(String stringLengthValidatorField) {
        this.stringLengthValidatorField = stringLengthValidatorField;

    public String getFieldExpressionValidatorField() {
        return fieldExpressionValidatorField;

    public void setFieldExpressionValidatorField(
            String fieldExpressionValidatorField) {
        this.fieldExpressionValidatorField = fieldExpressionValidatorField;

    public String getUrlValidatorField() {
        return urlValidatorField;

    public void setUrlValidatorField(String urlValidatorField) {
        this.urlValidatorField = urlValidatorField;