Class ValidationInterceptor
- All Implemented Interfaces:
Serializable
,ConditionalInterceptor
,Interceptor
- Direct Known Subclasses:
AnnotationValidationInterceptor
This interceptor runs the action through the standard validation framework, which in turn checks the action against
any validation rules (found in files such as ActionClass-validation.xml) and adds field-level and action-level
error messages (provided that the action implements ValidationAware
). This interceptor
is often one of the last (or second to last) interceptors applied in a stack, as it assumes that all values have
already been set on the action.
This interceptor does nothing if the name of the method being invoked is specified in the excludeMethods parameter. excludeMethods accepts a comma-delimited list of method names. For example, requests to foo!input.action and foo!back.action will be skipped by this interceptor if you set the excludeMethods parameter to "input, back".
The workflow of the action request does not change due to this interceptor. Rather, this interceptor is often used in conjunction with the workflow interceptor.
NOTE: As this method extends off MethodFilterInterceptor, it is capable of
deciding if it is applicable only to selective methods in the action class. See
MethodFilterInterceptor
for more info.
Interceptor parameters:
- alwaysInvokeValidate - Defaults to true. If true validate() method will always be invoked, otherwise it will not.
- programmatic - Defaults to true. If true and the action is Validateable call validate(), and any method that starts with "validate".
- declarative - Defaults to true. Perform validation based on xml or annotations.
Extending the interceptor:
There are no known extension points for this interceptor.Example code:
<action name="someAction" class="com.examples.SomeAction"> <interceptor-ref name="params"/> <interceptor-ref name="validation"/> <interceptor-ref name="workflow"/> <result name="success">good_result.ftl</result> </action> <-- in the following case myMethod of the action class will not get validated --> <action name="someAction" class="com.examples.SomeAction"> <interceptor-ref name="params"/> <interceptor-ref name="validation"> <param name="excludeMethods">myMethod</param> </interceptor-ref> <interceptor-ref name="workflow"/> <result name="success">good_result.ftl</result> </action> <-- in the following case only annotated methods of the action class will be validated --> <action name="someAction" class="com.examples.SomeAction"> <interceptor-ref name="params"/> <interceptor-ref name="validation"> <param name="validateAnnotatedMethodOnly">true</param> </interceptor-ref> <interceptor-ref name="workflow"/> <result name="success">good_result.ftl</result> </action>
- Author:
- Jason Carreira, Rainer Hermanns, Alexandru Popescu
- See Also:
-
Field Summary
Fields inherited from class org.apache.struts2.interceptor.MethodFilterInterceptor
excludeMethods, includeMethods
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
doBeforeInvocation
(ActionInvocation invocation) Gets the current action and its context and delegates toActionValidatorManager
proper validate method.protected String
doIntercept
(ActionInvocation invocation) Subclasses must override to implement the interceptor logic.protected String
getValidationContext
(ActionProxy proxy) Returns the context that will be used by theActionValidatorManager
to associate the action invocation with the appropriateValidatorConfigs
.boolean
Gets ifvalidate()
should always be called or only per annotated method.void
void
setAlwaysInvokeValidate
(String alwaysInvokeValidate) Determines ifValidateable
'svalidate()
should always be invoked.void
setDeclarative
(boolean declarative) Determines if validation based on annotations or xml should be performed.void
setProgrammatic
(boolean programmatic) Determines ifValidateable
'svalidate()
should be called, as well as methods whose name that start with "validate".void
setValidateAnnotatedMethodOnly
(boolean validateAnnotatedMethodOnly) Determine ifvalidate()
should always be called or only per annotated method.Methods inherited from class org.apache.struts2.interceptor.MethodFilterInterceptor
applyInterceptor, getExcludeMethodsSet, getIncludeMethodsSet, intercept, setExcludeMethods, setIncludeMethods
Methods inherited from class org.apache.struts2.interceptor.AbstractInterceptor
destroy, init, setDisabled, shouldIntercept
-
Constructor Details
-
ValidationInterceptor
public ValidationInterceptor()
-
-
Method Details
-
setActionValidatorManager
-
setProgrammatic
public void setProgrammatic(boolean programmatic) Determines ifValidateable
'svalidate()
should be called, as well as methods whose name that start with "validate". Defaults to "true".- Parameters:
programmatic
- true thenvalidate()
is invoked.
-
setDeclarative
public void setDeclarative(boolean declarative) Determines if validation based on annotations or xml should be performed. Defaults to "true".- Parameters:
declarative
- true then perform validation based on annotations or xml.
-
setAlwaysInvokeValidate
Determines ifValidateable
'svalidate()
should always be invoked. Default to "true".- Parameters:
alwaysInvokeValidate
- true thenvalidate()
is always invoked.
-
isValidateAnnotatedMethodOnly
public boolean isValidateAnnotatedMethodOnly()Gets ifvalidate()
should always be called or only per annotated method.- Returns:
- true to only validate per annotated method, otherwise false to always validate.
-
setValidateAnnotatedMethodOnly
public void setValidateAnnotatedMethodOnly(boolean validateAnnotatedMethodOnly) Determine ifvalidate()
should always be called or only per annotated method. Default to false.- Parameters:
validateAnnotatedMethodOnly
- true to only validate per annotated method, otherwise false to always validate.
-
doBeforeInvocation
Gets the current action and its context and delegates toActionValidatorManager
proper validate method.- Parameters:
invocation
- the execution state of the Action.- Throws:
Exception
- if an error occurs validating the action.
-
doIntercept
Description copied from class:MethodFilterInterceptor
Subclasses must override to implement the interceptor logic.- Specified by:
doIntercept
in classMethodFilterInterceptor
- Parameters:
invocation
- the action invocation- Returns:
- the result of invocation
- Throws:
Exception
- in case of any errors
-
getValidationContext
Returns the context that will be used by the
ActionValidatorManager
to associate the action invocation with the appropriateValidatorConfigs
.The context returned is used in the pattern ActionClass-context-validation.xml
The default context is the action name from the URL, but the method can be overridden to implement custom contexts.
This can be useful in cases in which a single action and a single model require vastly different validation based on some condition.
- Parameters:
proxy
- action proxy- Returns:
- the context (action name)
-