Class AnnotationValidationInterceptor2

java.lang.Object
org.apache.struts2.interceptor.AbstractInterceptor
com.opensymphony.xwork2.interceptor.AbstractInterceptor
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor
name.matthewgreet.strutscommons.interceptor.AnnotationValidationInterceptor2
All Implemented Interfaces:
com.opensymphony.xwork2.interceptor.ConditionalInterceptor, com.opensymphony.xwork2.interceptor.Interceptor, Serializable, org.apache.struts2.interceptor.ConditionalInterceptor, org.apache.struts2.interceptor.Interceptor

public class AnnotationValidationInterceptor2 extends com.opensymphony.xwork2.interceptor.MethodFilterInterceptor

Struts 2 Interceptor for setting form fields from request parameters, adjusting, converting, validating, and writing error messages where data type conversion fails, according to form field annotations. If the Action implements ModelDriven, the model is recognised as the form, not the action. Struts Actions are expected to implement ValidationAware and, if message keys are used, implement TextProvider.

Annotations are applied to a form's member variables. The standard set are found in name.matthewgreet.strutscommons.annotation and includes customisable ones. Annotations configure various kinds of policies and typically don't apply for missing form field values (@Required being a notable exception).

Types of Annotations
Policy Example Description
Adjuster @ToUpperCase Modifies form field value before validation or conversion.
Non-conversion validator @MaxLength Validates form field value before any conversion. Usually only applicable to string fields, with @Required as a notable exception.
Converter @DateConversion Converts request parameter from string to other data type.
Post-conversion validator @IntegerRange Validates converted form field value.
Manual parameter conversion @ManualParameterConversion A special annotation indicating form field is processed in manual parameter conversion mode (see below).

Each form field is processed in one of the modes described below.

Validation Modes
Mode Description
Auto conversion Directly sets form field if it passes non-conversion validation and conversion.
Default conversion Same as auto conversion but a default converter is used for the field's data type.
Manual parameter conversion Adjustment, conversion, and validation is done by form code. Form must implement Form.
Pair conversion Sets a string form field and applies adjusters and validators and converts to a paired, non-string form field.
No conversion Sets a string form field and applies adjusters and validators.
Set only Sets a string form field and no policies are applied.

Except where a @ManualParameterConversion annotation is used, how each mode applies is summarised below.

Mode Usage
Field type Converter annotation Mode Notes
String No No conversion
String Yes Pair conversion
Non-string No Default conversion
Non-string Yes Auto conversion
String array   Set only Converters don't apply to arrays
String collection No Set only
String collection Yes Auto conversion
Non-string array   Manual parameter conversion Converters don't apply to arrays
Non-string collection No Default conversion Few default converters for collection types exist
Non-string collection Yes Auto conversion

This replaces the standard AnnotationValidationInterceptor, ConversionErrorInterceptor and ParametersInterceptor and differs in various aspects.

  • Unlike ParametersInterceptor, directly sets form fields, rather than setting properties on the Value Stack.
  • Unlike ParametersInterceptor, does not convert multiple request parameters with the same name. See above.
  • Unlike ConversionErrorInterceptor, error messages aren't restricted to field errors.

Interceptor parameters:

disabled
If true, this interceptor is skipped.
paramNameMaxLength
Ignores all request parameters with names longer than this.

Extending the interceptor:

Various points of the core algorithm can be overridden to customize behaviour. See javadoc of protected methods.

Example code:

 <action name="someAction" class="com.examples.SomeAction">
     <interceptor-ref name="annotationValidation"/>
     <interceptor-ref name="validation"/>
     <result name="success">good_result.ftl</result>
 </action>
 
 
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface com.opensymphony.xwork2.interceptor.ConditionalInterceptor

    com.opensymphony.xwork2.interceptor.ConditionalInterceptor.LegacyAdapter
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final int
     

    Fields inherited from class com.opensymphony.xwork2.interceptor.MethodFilterInterceptor

    excludeMethods, includeMethods
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected String
    doIntercept(com.opensymphony.xwork2.ActionInvocation invocation)
     
    boolean
     
    int
     
     
    void
    setDisabled(boolean disabled)
     
    void
    setParamNameMaxLength(int paramNameMaxLength)
     

    Methods inherited from class com.opensymphony.xwork2.interceptor.MethodFilterInterceptor

    applyInterceptor, getExcludeMethodsSet, getIncludeMethodsSet, intercept, setExcludeMethods, setIncludeMethods

    Methods inherited from class com.opensymphony.xwork2.interceptor.AbstractInterceptor

    intercept, shouldIntercept, shouldIntercept

    Methods inherited from class org.apache.struts2.interceptor.AbstractInterceptor

    destroy, init, setDisabled

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.apache.struts2.interceptor.Interceptor

    destroy, init
  • Field Details

    • PARAM_NAME_MAX_LENGTH

      protected static final int PARAM_NAME_MAX_LENGTH
      See Also:
  • Constructor Details

    • AnnotationValidationInterceptor2

      public AnnotationValidationInterceptor2()
  • Method Details

    • doIntercept

      protected String doIntercept(com.opensymphony.xwork2.ActionInvocation invocation) throws Exception
      Specified by:
      doIntercept in class com.opensymphony.xwork2.interceptor.MethodFilterInterceptor
      Throws:
      Exception
    • makeAnnotationValidationLibrary

      protected AnnotationValidatior makeAnnotationValidationLibrary()
    • getDisabled

      public boolean getDisabled()
    • setDisabled

      public void setDisabled(boolean disabled)
    • getParamNameMaxLength

      public int getParamNameMaxLength()
    • setParamNameMaxLength

      public void setParamNameMaxLength(int paramNameMaxLength)