Class AnnotationValidationInterceptor2
- All Implemented Interfaces:
ConditionalInterceptor,Interceptor,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. |
| 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.
| 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.
| 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. Defaults to false.
- paramNameMaxLength
- Ignores all request parameters with names longer than this. Defaults to 100.
- processUnreferencedFields
- If true, does not skip fields that have no policy annotations and aren't set by a parameter. This will only be useful where a default converter must still process missing values. Defaults to false.
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:
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.opensymphony.xwork2.interceptor.ConditionalInterceptor
ConditionalInterceptor.LegacyAdapter -
Field Summary
FieldsFields inherited from class com.opensymphony.xwork2.interceptor.MethodFilterInterceptor
excludeMethods, includeMethods -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringdoIntercept(ActionInvocation invocation) booleanintbooleanprotected 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.voidsetDisabled(boolean disabled) voidsetParamNameMaxLength(int paramNameMaxLength) voidsetProcessUnreferencedFields(boolean processUnreferencedFields) Methods inherited from class com.opensymphony.xwork2.interceptor.MethodFilterInterceptor
applyInterceptor, getExcludeMethodsSet, getIncludeMethodsSet, intercept, setExcludeMethods, setIncludeMethodsMethods inherited from class com.opensymphony.xwork2.interceptor.AbstractInterceptor
intercept, shouldIntercept, shouldInterceptMethods inherited from class org.apache.struts2.interceptor.AbstractInterceptor
destroy, init, setDisabledMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.struts2.interceptor.Interceptor
destroy, init
-
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. -
getDisabled
public boolean getDisabled() -
setDisabled
public void setDisabled(boolean disabled) -
getParamNameMaxLength
public int getParamNameMaxLength() -
setParamNameMaxLength
public void setParamNameMaxLength(int paramNameMaxLength) -
getProcessUnreferencedFields
public boolean getProcessUnreferencedFields() -
setProcessUnreferencedFields
public void setProcessUnreferencedFields(boolean processUnreferencedFields)
-