Class AnnotationValidationInterceptor
- All Implemented Interfaces:
com.opensymphony.xwork2.interceptor.Interceptor
,Serializable
Struts 2 Interceptor for parsing annotated form fields from strings to another data type and writes error
messages where data type conversion fails. 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
.
This differs from the standard ConversionErrorInterceptor by allowing writing to the general error list, not just field errors, and, if a message key is used, using the Action's text provider directly, not going through the Value Stack.
Interceptor parameters:
- None
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 ClassesModifier and TypeClassDescriptionstatic class
Describes a form field annotation, its type, and an instance of a policy it configures, if applicable.static class
Describes the result of a attempting to convert from a string to the generic type, and the field to receive it, successful or not. -
Field Summary
Fields inherited from class com.opensymphony.xwork2.interceptor.MethodFilterInterceptor
excludeMethods, includeMethods
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> void
checkCollectionConversionMessage
(Field unconvertedField, Annotation annotation, CollectionConverter<?, T> collectionConverter, AnnotationValidationInterceptor.ConversionFieldResult<T> conversionFieldResult, com.opensymphony.xwork2.interceptor.ValidationAware validationAware, com.opensymphony.xwork2.TextProvider textProvider) Checks if conversion results means a message must be written and writes appropriate type if needed.protected boolean
checkCollectionConversionRecipientDataType
(Field unconvertedField, Annotation annotation, Field recipientField, Class<?> recipientClass) Returns whether converting validator can convert form field value to the recipient field's type.protected <T> void
checkCollectionPostConversionMessage
(Field unconvertedField, Annotation annotation, CollectionPostConversionValidator<?, T> collectionPostConversionValidator, AnnotationValidationInterceptor.ConversionFieldResult<T> conversionFieldResult, ValidationResult validationResult, com.opensymphony.xwork2.interceptor.ValidationAware validationAware, com.opensymphony.xwork2.TextProvider textProvider) Checks if conversion results means a message must be written and writes appropriate type if needed.protected boolean
checkCollectionPostConversionRecipientDataType
(Annotation annotation, Field recipientField, Class<?> recipientClass) Returns whether post conversion validator can use the recipient field's type.protected <T> void
checkConversionMessage
(Field unconvertedField, Annotation annotation, Converter<?, T> converter, AnnotationValidationInterceptor.ConversionFieldResult<T> conversionFieldResult, com.opensymphony.xwork2.interceptor.ValidationAware validationAware, com.opensymphony.xwork2.TextProvider textProvider) Checks if conversion results means a message must be written and writes appropriate type if needed.protected boolean
checkConversionRecipientDataType
(Field unconvertedField, Annotation annotation, Field recipientField, Class<?> recipientClass) Returns whether converting validator can convert form field value to the recipient field's type.protected void
checkNonConversionMessage
(Field unconvertedField, Annotation annotation, NonConversionValidator<?> validator, ValidationResult validationResult, com.opensymphony.xwork2.interceptor.ValidationAware validationAware, com.opensymphony.xwork2.TextProvider textProvider) Checks if non-conversion validation results means a message must be written and writes appropriate type if needed.protected <T> void
checkPostConversionMessage
(Field unconvertedField, Annotation annotation, PostConversionValidator<?, T> postConversionValidator, AnnotationValidationInterceptor.ConversionFieldResult<T> conversionFieldResult, ValidationResult validationResult, com.opensymphony.xwork2.interceptor.ValidationAware validationAware, com.opensymphony.xwork2.TextProvider textProvider) Checks if conversion results means a message must be written and writes appropriate type if needed.protected boolean
checkPostConversionRecipientDataType
(Annotation annotation, Field recipientField, Class<?> recipientClass) Returns whether post conversion validator can use the recipient field's type.protected <T> AnnotationValidationInterceptor.ConversionFieldResult
<T> collectionConvert
(com.opensymphony.xwork2.ActionInvocation invocation, Object form, Collection<Field> allFormFields, Field formField, String fieldName, String fieldValue, AnnotationValidationInterceptor.AnnotationEntry<T> collectionConversionAnnotationEntry, boolean modelDriven) Converts form field value and returns result, or null if recipient field missing or compatibility checks fail.protected <T> ValidationResult
collectionPostConvert
(com.opensymphony.xwork2.ActionInvocation invocation, Object form, Field formField, Field recipientField, Collection<T> parsedValue, AnnotationValidationInterceptor.AnnotationEntry<T> postConversionAnnotationEntry, boolean modelDriven) Return validation result of converted form field value, or null if compatibility checks fail.protected <T> AnnotationValidationInterceptor.ConversionFieldResult
<T> convert
(com.opensymphony.xwork2.ActionInvocation invocation, Object form, Collection<Field> allFormFields, Field formField, String fieldName, String fieldValue, AnnotationValidationInterceptor.AnnotationEntry<T> conversionAnnotationEntry, boolean modelDriven) Converts form field value and returns result, or null if recipient field missing or compatibility checks fail.protected String
doIntercept
(com.opensymphony.xwork2.ActionInvocation invocation) protected <T> ValidatorLibrary.AnnotationUsageResult
<T> getAnnotationUsage
(Field unconvertedField, Annotation annotation) Returns validator (and formatter) that processes an annotated form field, or result of NA type if not recognised.protected <T> boolean
getCollectionPostConversionShortCircuit
(Field unconvertedField, AnnotationValidationInterceptor.AnnotationEntry<?> annotationEntry, Collection<T> parsedValue) Returns whether a rejection by the non-conversion validator should stop further validation.protected String
getDefaultRecipientName
(Field unconvertedField, String fieldName) Returns the default field name to receive a converted form field.protected String
getMessage
(com.opensymphony.xwork2.TextProvider textProvider, String messageKey, String message) Returns message to use, extracted from text provider if required.protected boolean
getNonConversionShortCircuit
(Field unconvertedField, AnnotationValidationInterceptor.AnnotationEntry<?> annotationEntry, String formValue) Returns whether a rejection by the non-conversion validator should stop further validation.protected <T> boolean
getPostConversionShortCircuit
(Field unconvertedField, AnnotationValidationInterceptor.AnnotationEntry<?> annotationEntry, T parsedValue) Returns whether a rejection by the non-conversion validator should stop further validation.static Collection
<Field> getProperties
(Class<?> type) Returns all member fields of a class, whether directly declared by the class or inherited.protected Field
getRecipientField
(Field unconvertedField, Annotation annotation, Collection<Field> fields, String recipientFieldName) Finds field to receive converted value from fields found in the form, or null if not found.protected <T> Collection
<T> makeCollectionForRecipient
(Class<?> recipientFieldClass) Returns an empty collection to set the recipient field and will accept converted values.protected <T> ValidationResult
postConvert
(com.opensymphony.xwork2.ActionInvocation invocation, Object form, Field formField, Field recipientField, T parsedValue, AnnotationValidationInterceptor.AnnotationEntry<T> postConversionAnnotationEntry, boolean modelDriven) Return validation result of converted form field value, or null if compatibility checks fail.protected <T> ConversionResult
<T> validateCollectionConversion
(Field unconvertedField, Annotation annotation, CollectionConverter<?, T> collectionConversionValidator, String formValue, Field recipientField, Class<T> recipientClass) Calls validator to convert string form field value to recipient field of expected data type.protected <T> ValidationResult
validateCollectionPostConversion
(Field unconvertedField, Annotation annotation, CollectionPostConversionValidator<?, T> collectionPostConversionValidator, Collection<T> convertedValue, Field recipientField, Class<?> recipientClass) Calls validator to check a converted form field.protected <T> ConversionResult
<T> validateConversion
(Field unconvertedField, Annotation annotation, Converter<?, T> conversionValidator, String formValue, Field recipientField, Class<T> recipientClass) Calls validator to convert string form field value to recipient field of expected data type.protected ValidationResult
validateNonConversion
(Field unconvertedField, AnnotationValidationInterceptor.AnnotationEntry<?> annotationEntry, String formValue) Calls validator to check a form field before any conversion.protected <T> ValidationResult
validatePostConversion
(Field unconvertedField, Annotation annotation, PostConversionValidator<?, T> postConversionValidator, T convertedValue, Field recipientField, Class<T> recipientClass) Calls validator to check a converted form field.protected void
writeFieldError
(com.opensymphony.xwork2.interceptor.ValidationAware validationAware, String fieldName, String message) Writes message to Action's field errors for field.protected void
writeGeneralError
(com.opensymphony.xwork2.interceptor.ValidationAware validationAware, String message) Writes message to Action's action errors.protected void
writeInfoMessage
(com.opensymphony.xwork2.interceptor.ValidationAware validationAware, String message) Writes message to Action's action messages.protected void
writeWarningMessage
(com.opensymphony.xwork2.interceptor.ValidationAware validationAware, String message) Writes message to Action's action warnings.Methods inherited from class com.opensymphony.xwork2.interceptor.MethodFilterInterceptor
applyInterceptor, getExcludeMethodsSet, getIncludeMethodsSet, intercept, setExcludeMethods, setIncludeMethods
Methods inherited from class com.opensymphony.xwork2.interceptor.AbstractInterceptor
destroy, init
-
Constructor Details
-
AnnotationValidationInterceptor
public AnnotationValidationInterceptor()
-
-
Method Details
-
getProperties
Returns all member fields of a class, whether directly declared by the class or inherited. -
checkCollectionConversionMessage
protected <T> void checkCollectionConversionMessage(Field unconvertedField, Annotation annotation, CollectionConverter<?, T> collectionConverter, AnnotationValidationInterceptor.ConversionFieldResult<T> conversionFieldResult, com.opensymphony.xwork2.interceptor.ValidationAware validationAware, com.opensymphony.xwork2.TextProvider textProvider) Checks if conversion results means a message must be written and writes appropriate type if needed. -
checkCollectionConversionRecipientDataType
protected boolean checkCollectionConversionRecipientDataType(Field unconvertedField, Annotation annotation, Field recipientField, Class<?> recipientClass) Returns whether converting validator can convert form field value to the recipient field's type. -
checkCollectionPostConversionMessage
protected <T> void checkCollectionPostConversionMessage(Field unconvertedField, Annotation annotation, CollectionPostConversionValidator<?, T> collectionPostConversionValidator, AnnotationValidationInterceptor.ConversionFieldResult<T> conversionFieldResult, ValidationResult validationResult, com.opensymphony.xwork2.interceptor.ValidationAware validationAware, com.opensymphony.xwork2.TextProvider textProvider) Checks if conversion results means a message must be written and writes appropriate type if needed. -
checkCollectionPostConversionRecipientDataType
protected boolean checkCollectionPostConversionRecipientDataType(Annotation annotation, Field recipientField, Class<?> recipientClass) Returns whether post conversion validator can use the recipient field's type. -
checkConversionMessage
protected <T> void checkConversionMessage(Field unconvertedField, Annotation annotation, Converter<?, T> converter, AnnotationValidationInterceptor.ConversionFieldResult<T> conversionFieldResult, com.opensymphony.xwork2.interceptor.ValidationAware validationAware, com.opensymphony.xwork2.TextProvider textProvider) Checks if conversion results means a message must be written and writes appropriate type if needed. -
checkConversionRecipientDataType
protected boolean checkConversionRecipientDataType(Field unconvertedField, Annotation annotation, Field recipientField, Class<?> recipientClass) Returns whether converting validator can convert form field value to the recipient field's type. -
checkNonConversionMessage
protected void checkNonConversionMessage(Field unconvertedField, Annotation annotation, NonConversionValidator<?> validator, ValidationResult validationResult, com.opensymphony.xwork2.interceptor.ValidationAware validationAware, com.opensymphony.xwork2.TextProvider textProvider) Checks if non-conversion validation results means a message must be written and writes appropriate type if needed. -
checkPostConversionRecipientDataType
protected boolean checkPostConversionRecipientDataType(Annotation annotation, Field recipientField, Class<?> recipientClass) Returns whether post conversion validator can use the recipient field's type. -
checkPostConversionMessage
protected <T> void checkPostConversionMessage(Field unconvertedField, Annotation annotation, PostConversionValidator<?, T> postConversionValidator, AnnotationValidationInterceptor.ConversionFieldResult<T> conversionFieldResult, ValidationResult validationResult, com.opensymphony.xwork2.interceptor.ValidationAware validationAware, com.opensymphony.xwork2.TextProvider textProvider) Checks if conversion results means a message must be written and writes appropriate type if needed. -
collectionConvert
protected <T> AnnotationValidationInterceptor.ConversionFieldResult<T> collectionConvert(com.opensymphony.xwork2.ActionInvocation invocation, Object form, Collection<Field> allFormFields, Field formField, String fieldName, String fieldValue, AnnotationValidationInterceptor.AnnotationEntry<T> collectionConversionAnnotationEntry, boolean modelDriven) Converts form field value and returns result, or null if recipient field missing or compatibility checks fail. -
collectionPostConvert
protected <T> ValidationResult collectionPostConvert(com.opensymphony.xwork2.ActionInvocation invocation, Object form, Field formField, Field recipientField, Collection<T> parsedValue, AnnotationValidationInterceptor.AnnotationEntry<T> postConversionAnnotationEntry, boolean modelDriven) Return validation result of converted form field value, or null if compatibility checks fail. -
convert
protected <T> AnnotationValidationInterceptor.ConversionFieldResult<T> convert(com.opensymphony.xwork2.ActionInvocation invocation, Object form, Collection<Field> allFormFields, Field formField, String fieldName, String fieldValue, AnnotationValidationInterceptor.AnnotationEntry<T> conversionAnnotationEntry, boolean modelDriven) Converts form field value and returns result, or null if recipient field missing or compatibility checks fail. -
doIntercept
- Specified by:
doIntercept
in classcom.opensymphony.xwork2.interceptor.MethodFilterInterceptor
- Throws:
Exception
-
getAnnotationUsage
protected <T> ValidatorLibrary.AnnotationUsageResult<T> getAnnotationUsage(Field unconvertedField, Annotation annotation) throws Exception Returns validator (and formatter) that processes an annotated form field, or result of NA type if not recognised.- Throws:
Exception
-
getCollectionPostConversionShortCircuit
protected <T> boolean getCollectionPostConversionShortCircuit(Field unconvertedField, AnnotationValidationInterceptor.AnnotationEntry<?> annotationEntry, Collection<T> parsedValue) Returns whether a rejection by the non-conversion validator should stop further validation. -
getDefaultRecipientName
Returns the default field name to receive a converted form field. -
getMessage
protected String getMessage(com.opensymphony.xwork2.TextProvider textProvider, String messageKey, String message) Returns message to use, extracted from text provider if required. textProvider can be null if Action does not implement it. -
getNonConversionShortCircuit
protected boolean getNonConversionShortCircuit(Field unconvertedField, AnnotationValidationInterceptor.AnnotationEntry<?> annotationEntry, String formValue) Returns whether a rejection by the non-conversion validator should stop further validation. -
getPostConversionShortCircuit
protected <T> boolean getPostConversionShortCircuit(Field unconvertedField, AnnotationValidationInterceptor.AnnotationEntry<?> annotationEntry, T parsedValue) Returns whether a rejection by the non-conversion validator should stop further validation. -
getRecipientField
protected Field getRecipientField(Field unconvertedField, Annotation annotation, Collection<Field> fields, String recipientFieldName) Finds field to receive converted value from fields found in the form, or null if not found. -
makeCollectionForRecipient
Returns an empty collection to set the recipient field and will accept converted values. -
postConvert
protected <T> ValidationResult postConvert(com.opensymphony.xwork2.ActionInvocation invocation, Object form, Field formField, Field recipientField, T parsedValue, AnnotationValidationInterceptor.AnnotationEntry<T> postConversionAnnotationEntry, boolean modelDriven) Return validation result of converted form field value, or null if compatibility checks fail. -
validateCollectionConversion
protected <T> ConversionResult<T> validateCollectionConversion(Field unconvertedField, Annotation annotation, CollectionConverter<?, T> collectionConversionValidator, String formValue, Field recipientField, Class<T> recipientClass) Calls validator to convert string form field value to recipient field of expected data type. -
validateCollectionPostConversion
protected <T> ValidationResult validateCollectionPostConversion(Field unconvertedField, Annotation annotation, CollectionPostConversionValidator<?, T> collectionPostConversionValidator, Collection<T> convertedValue, Field recipientField, Class<?> recipientClass) Calls validator to check a converted form field. -
validateConversion
protected <T> ConversionResult<T> validateConversion(Field unconvertedField, Annotation annotation, Converter<?, T> conversionValidator, String formValue, Field recipientField, Class<T> recipientClass) Calls validator to convert string form field value to recipient field of expected data type. -
validateNonConversion
protected ValidationResult validateNonConversion(Field unconvertedField, AnnotationValidationInterceptor.AnnotationEntry<?> annotationEntry, String formValue) Calls validator to check a form field before any conversion. -
validatePostConversion
protected <T> ValidationResult validatePostConversion(Field unconvertedField, Annotation annotation, PostConversionValidator<?, T> postConversionValidator, T convertedValue, Field recipientField, Class<T> recipientClass) Calls validator to check a converted form field. -
writeFieldError
protected void writeFieldError(com.opensymphony.xwork2.interceptor.ValidationAware validationAware, String fieldName, String message) Writes message to Action's field errors for field. ValidationAware can be null if Action does not implement it. -
writeGeneralError
protected void writeGeneralError(com.opensymphony.xwork2.interceptor.ValidationAware validationAware, String message) Writes message to Action's action errors. ValidationAware can be null if Action does not implement it. -
writeInfoMessage
protected void writeInfoMessage(com.opensymphony.xwork2.interceptor.ValidationAware validationAware, String message) Writes message to Action's action messages. ValidationAware can be null if Action does not implement it. -
writeWarningMessage
protected void writeWarningMessage(com.opensymphony.xwork2.interceptor.ValidationAware validationAware, String message) Writes message to Action's action warnings. ValidationAware can be null if Action does not implement it.
-