Class AnnotationValidationInterceptor2
- All Implemented Interfaces:
Serializable,ConditionalInterceptor,Interceptor
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).
| 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. |
| List converter | @IntegerCSVConversion | Splits and converts string request parameter and sets array or collection form field. |
| List post-conversion adjuster | None | Modifies converted values of array or collection form field. |
| List post-conversion validator | @RequiredIntegerEntries | Validates converted values of array or 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.
| 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. |
Except where a @ManualParameterConversion annotation is used, how each mode applies is summarised below.
| Field type | Converter annotation | Mode | Notes |
|---|---|---|---|
| String | No | No conversion | |
| String | Single value | Pair conversion | |
| Non-string | No | Default conversion | |
| Non-string | Single value | Auto conversion | |
| String array or collection | List | Auto conversion | List converters split a single parameter into multiple entries |
| Non-string array or collection | No | Default conversion | Expects multiple parameters sharing name, applying default converter to each |
| Non-string array or collection | Single value | Auto conversion | Expects multiple parameters sharing name, applying converter to each |
| Non-string array or collection | List | Auto conversion | List converters split a single parameter into multiple entries |
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 ConversionErrorInterceptor, error messages aren't restricted to field errors.
Interceptor parameters:
- defaultMessageType
- Where to write conversion and validation failures where an annotation uses
Required.MessageTypeof 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.
- paramMaxListSize
- Maximum number entries of an array or collection field. Defaults to 256.
- 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 Summary
FieldsFields inherited from class org.apache.struts2.interceptor.MethodFilterInterceptor
excludeMethods, includeMethods -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringdoIntercept(ActionInvocation invocation) booleanbooleanintReturns maximum number entries of an array or collection field.intprotected AnnotationValidatiorCreates and returns helper that validations form fields according to their annotations.protected PolicyLookupCreates and returns helper that finds a policy implementation from a form field adjuster, converter, or validator annotation.voidsetDefaultMessageType(Required.MessageType defaultMessageType) voidsetDisabled(boolean disabled) voidsetIgnoreNonAnnotatedFields(boolean ignoreNonAnnotatedFields) voidsetParamMaxListSize(int paramMaxListSize) voidsetParamNameMaxLength(int paramNameMaxLength) Methods inherited from class org.apache.struts2.interceptor.MethodFilterInterceptor
applyInterceptor, getExcludeMethodsSet, getIncludeMethodsSet, intercept, setExcludeMethods, setIncludeMethodsMethods inherited from class org.apache.struts2.interceptor.AbstractInterceptor
destroy, init, setDisabled, shouldIntercept
-
Field Details
-
PARAM_NAME_MAX_LENGTH
public static final int PARAM_NAME_MAX_LENGTH- See Also:
-
-
Constructor Details
-
AnnotationValidationInterceptor2
public AnnotationValidationInterceptor2()
-
-
Method Details
-
doIntercept
- Specified by:
doInterceptin classMethodFilterInterceptor- Throws:
Exception
-
makeAnnotationValidationLibrary
Creates and returns helper that validations form fields according to their annotations. -
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
-
setDefaultMessageType
-
getDisabled
public boolean getDisabled() -
setDisabled
public void setDisabled(boolean disabled) -
getIgnoreNonAnnotatedFields
public boolean getIgnoreNonAnnotatedFields() -
setIgnoreNonAnnotatedFields
public void setIgnoreNonAnnotatedFields(boolean ignoreNonAnnotatedFields) -
getParamMaxListSize
public int getParamMaxListSize()Returns maximum number entries of an array or collection field. Defaults to 256. -
setParamMaxListSize
public void setParamMaxListSize(int paramMaxListSize) -
getParamNameMaxLength
public int getParamNameMaxLength() -
setParamNameMaxLength
public void setParamNameMaxLength(int paramNameMaxLength)
-