Class AnnotationValidationInterceptor2

All Implemented Interfaces:
ConditionalInterceptor, Interceptor, Serializable, ConditionalInterceptor, Interceptor

public class AnnotationValidationInterceptor2 extends 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 parameter value before validation or conversion.
Non-conversion validator @MaxLength Validates parameter value before any conversion. Usually only applicable to string fields, with @Required as a notable exception.
Converter @DateConversion Converts string request parameter and sets form field.
Post-conversion adjuster @ToStartOfDay Modifies converted form field value.
Post-conversion validator @IntegerRange Validates converted form field value.
Collection converter @IntegerCSVConversion Splits and converts string request parameter and sets collection form field.
Collection post-conversion adjuster None Modifies converted values of collection form field.
Collection post-conversion validator @RequiredIntegerEntries Validates converted values of collection form field.
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:

defaultMessageType
Where to write conversion and validation failures where an annotation uses Required.MessageType of DEFAULT. Cannot be DEFAULT. Defaults to ERROR.
disabled
If true, this interceptor is skipped. Defaults to false.
ignoreNonAnnotatedFields
If true, does not process form fields with no recognised annotations. This is used for viewer Actions where some member fields are used for display, not accept form parameters. Defaults to false.
paramNameMaxLength
Ignores all request parameters with names longer than this. Defaults to 100.

Extending the interceptor:

makeAnnotationValidationLibrary
Creates helper that adjust, validates and converts form field values.
makePolicyLookup
Creates helper that returns the policy implementation for a form field annotation.

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:
  • Field Details

  • Constructor Details

    • AnnotationValidationInterceptor2

      public AnnotationValidationInterceptor2()
  • Method Details

    • doIntercept

      protected String doIntercept(ActionInvocation invocation) throws Exception
      Specified by:
      doIntercept in class MethodFilterInterceptor
      Throws:
      Exception
    • makeAnnotationValidationLibrary

      protected AnnotationValidatior makeAnnotationValidationLibrary()
      Creates and returns helper that validations form fields according to their annotations.
    • makePolicyLookup

      protected PolicyLookup makePolicyLookup()
      Creates and returns helper that finds a policy implementation from a form field adjuster, converter, or validator annotation. Does not scan for non-built-in, non-custom policies if disabledClassScanner is true.
    • getDefaultMessageType

      public Required.MessageType getDefaultMessageType()
    • setDefaultMessageType

      public void setDefaultMessageType(Required.MessageType defaultMessageType)
    • getDisabled

      public boolean getDisabled()
    • setDisabled

      public void setDisabled(boolean disabled)
    • getIgnoreNonAnnotatedFields

      public boolean getIgnoreNonAnnotatedFields()
    • setIgnoreNonAnnotatedFields

      public void setIgnoreNonAnnotatedFields(boolean ignoreNonAnnotatedFields)
    • getParamNameMaxLength

      public int getParamNameMaxLength()
    • setParamNameMaxLength

      public void setParamNameMaxLength(int paramNameMaxLength)