Child pages
  • OVal Plugin
Skip to end of metadata
Go to start of metadata

Overview

OVal is a pragmatic and extensible validation framework for any kind of Java objects (not only JavaBeans). Constraints can be declared with annotations (@NotNull, @MaxLength), POJOs or XML.Custom constraints can be expressed as custom Java classes or by using scripting languages such as JavaScript, Groovy, BeanShell, OGNL or MVEL.

The OVal plugin provides support for using the OVal Validation Framework

Interceptors

The plugin defines the interceptor "ovalValidation" and the interceptor stack "ovalValidationStack" in the "oval-default" package. To use this interceptor, extend the "oval-default" package and apply the interceptor to your action, like:

Annotations

OVal provides many annotations for validations that can be used out of the box (custom validators can also be defined). Once the "ovalValidation" interceptor is applied to an action, you can annotate it:

Use the @AssertValid annotation to validate nested objects, like

XML Configuration

OVal provides support for defining the validation via XML. Validation files must end in "-validation.xml" and the rules to find them, are the same rules used to find the validation XML files used by the regular validation mechanisms (default validation in xwork):

  1. Per Action class: in a file named ActionName-validation.xml
  2. Per Action alias: in a file named ActionName-alias-validation.xml
  3. Inheritance hierarchy and interfaces implemented by Action class: The plugin searches up the inheritance tree of the action to find default validations for parent classes of the Action and interfaces implemented

Here is an example of an XML validation file:

Profiles

A profile is a set of validations, that can be enabled for a method in an action, for example:

In this example, when firstAndLast() is executed, the fields firstName and lastName will be validated. When middle() is executed, only middleName will be validated. When a method is annotated with the Profiles annotation, only the validations in the specified profiles will be performed. If no profile is specified for an action method, all the validations in the class will be evaluated.

Internationalization of messages

The OVal annotations (and corresponding XML tags) have a message attribute that can be used to specify either the error message, or the key of the error message. If a key is found in a property file, matching the value of the massage attribute, it will be used as the message, otherwise the value will be used as a literal string. For example, given this property file:

BookAction.properties

and this class:

BookAction.java

When that action is validated, the field errors would be:

  • "title cannot be null"
  • "You must enter a valid ISBN"
  • "title value is too long, allowed length is 3"

The current OVal "context" object is pushed into the stack for each validator, so it can be accessed from the property file to build the error message. See the OVal javadoc for more properties available in the FieldContext class.

The OVal Validation Interceptor

This interceptor runs the action through the standard validation framework, which in turn checks the action against any validation rules (found in files such as ActionClass-validation.xml) and adds field-level and action-level error messages (provided that the action implements com.opensymphony.xwork2.ValidationAware). This interceptor is often one of the last (or second to last) interceptors applied in a stack, as it assumes that all values have already been set on the action.

This interceptor does nothing if the name of the method being invoked is specified in the excludeMethods parameter. excludeMethods accepts a comma-delimited list of method names. For example, requests to foo!input.action and foo!back.action will be skipped by this interceptor if you set the excludeMethods parameter to "input, back".

Note that this has nothing to do with the com.opensymphony.xwork2.Validateable interface and simply adds error messages to the action. The workflow of the action request does not change due to this interceptor. Rather, this interceptor is often used in conjuction with the workflow interceptor.

NOTE: As this method extends off MethodFilterInterceptor, it is capable of deciding if it is applicable only to selective methods in the action class. See MethodFilterInterceptor for more info.

The param alwaysInvokeValidate (default to true), will make the interceptor invoke validate() on the action, if the action implements Validateable.

The param programmatic (defaults to true), will make the plugin call validateX() where X is the name of the method that will be invoked in the action. If this param is set to false, alwaysInvokeValidate is ignored and validate() won't be invoked.

Installation

The jar plugin needs to be added to the lib directory of your application as well as other dependencies. If you are using XML validation, XStream needs to be included. Here is the maven dependency example:

Configuration reference

Add a constant element to your struts config file to change the value of a configuration setting, like:

Name

Default Value

Description

struts.oval.validateJPAAnnotations

false

Enables mapping of JPA annotations to Oval validators

Resources

  • No labels

5 Comments

  1. Anonymous

    This is inform you that property "struts.oval.validateJPAAnnotations" is never set when using this plugin
    environment:
    Struts version = 2.3.4
    Object Factory = CDI
    Oval Plugin = 2.3.4
    ORM = Hibernate 3.6.10 as JPA provider
    Validation framework = Hibernate Validator.

    1. The easiest way to get this solved is to register an issue with JIRA https://issues.apache.org/jira/browse/WW

      1. I have reported the issue WW-3834 thanks for the info I would like to see this plugin work (smile)

          1. Thanks so much