Detailed description of struts2 18 Interceptor (18th)

Source: Internet
Author: User

The AnnotationValidationInterceptor interceptor is in the defaultStack 17th position and is mainly used for data verification. The interceptor inherits from the ValidationInterceptor interceptor and adds the function of canceling verification using Annotations on methods. ValidationInterceptor inherits from MethodFilterInterceptor. MethodFilterInterceptor is not mentioned in the interceptor inherited from MethodFilterInterceptor. In the AnnotationValidationInterceptor configuration, a parameter named excludeMethods is passed. This parameter is submitted to MethodFilterInterceptor, it is used to specify which methods do not require verification. So here we will first explain the MethodFilterInterceptor Interceptor. The following is the intercept source code of MethodFilterInterceptor: [java] @ Override public String intercept (ActionInvocation invocation) throws Exception {if )) {return doIntercept (invocation);} return invocation. invoke ();} invocation is executed here. invoke (); previously called applyInterceptor to determine whether to apply the Interceptor. Next let's take a look at the applyInterceptor method: [java] protected boolean applyInterceptor (actioninvocatati On invocation) {String method = invocation. getProxy (). getMethod (); // The true judgment method is MethodFilterInterceptorUtil. applyMethod: the set of excluded methods and the set of included methods and the name of the method to be executed by the Action. // The method converts the string to a regular expression to match the method. The logic is not difficult, but there are many codes to judge. So let's talk about this... boolean applyMethod = MethodFilterInterceptorUtil. applyMethod (excludeMethods, includeMethods, method); if (log. isDebugEnabled () {if (! ApplyMethod) {log. debug ("Skipping Interceptor... method ["+ method +"] found in exclude list. ") ;}} return applyMethod;} So all interceptors inherited from MethodFilterInterceptor can set the excludeMethods and includeMethods parameters to specify which methods should be applied to the interceptor, the methods do not need to be applied to the Interceptor. For AnnotationValidationInterceptor, which methods are required for verification and which methods are not required for verification. The configuration of AnnotationValidationInterceptor in defaultStack is: [html] <interceptor-ref name = "validation"> <param name = "excludeMethods"> input, back, cancel, browse </param> </interceptor-ref> is used to exclude the input, back, cancel, and browse methods. All other Action methods must be verified. Now we assume that the verification is required, so the doIntercept method of AnnotationValidationInterceptor will be executed. The source code of this method is as follows: [java] protected String doIntercept (ActionInvocation invocation) throws Exception {// get the currently executed Action Object action = invocation. getAction (); if (action! = Null) {// if Action is not null Method method = getActionMethod (action. getClass (), invocation. getProxy (). getMethod (); // obtain the Method to be executed by the Action // obtain the Collection of methods with the SkipValidation annotation added to the Action <Method> annotatedMethods = AnnotationUtils. getAnnotatedMethods (action. getClass (), SkipValidation. class); if (annotatedMethods. contains (method) return invocation. invoke (); // If the currently executed method has the SkipValidation annotation, no verification is performed and the next interceptor is called. // check whether the parent class is overwritten. Method Class clazz = action. getClass (). getSuperclass (); // obtain the parent Class bytecode while (clazz! = Null) {annotatedMethods = AnnotationUtils. getAnnotatedMethods (clazz, SkipValidation. class); // obtain the if (annotatedMethods! = Null) {// if the method is not null // if the method to be executed overwrites the method of the parent class, and the parent class method is marked with the SkipValidation annotation, the current Method is not verified for (Method annotatedMethod: annotatedMethods) {if (annotatedMethod. getName (). equals (method. getName () & Arrays. equals (annotatedMethod. getParameterTypes (), method. getParameterTypes () & Arrays. equals (annotatedMethod. getExceptionTypes (), method. getExceptionTypes () return invocation. invoke (); // call the next interceptor} clazz = c Lazz. getSuperclass (); // obtain the parent class bytecode} // if you want to perform verification, continue to call the doIntercept method return super of the parent class. doIntercept (invocation);} From the above we can see that if the method executed by the current Action is marked with the SkipValidation annotation or the method covered with the SkipValidation annotation, the method will not be verified, after the execution is complete, the doIntercept method of the ValidationInterceptor class is also called. The source code of this method is [java] @ Override protected String doIntercept (ActionInvocation invocation) throws Exception {doBeforeInvocation ); // call doBeforeInvocation method retu Rn invocation. invoke (); // call the next interceptor} doBeforeInvocation (invocation) method source code: protected void doBeforeInvocation (ActionInvocation invocation) throws Exception {Object action = invocation. getAction (); // get the currently executed Action ActionProxy proxy = invocation. getProxy (); // get the ActionProxy object // the action name has to be from the url, otherwise validators that use aliases, like // the MyActio-someaction-validator.xml will not B E found, see WW-3194 String context = proxy. getActionName (); // get the Action name String method = proxy. getMethod (); // get the name of the method that executes the Action // omit... // declarative defaults to true if (declarative) {if (validateAnnotatedMethodOnly) {// validateAnnotatedMethodOnly defaults to false actionValidatorManager. validate (action, context, method);} else {actionValidatorManager. validate (action, context); // so execute here} // if the Action implements the Validateable Interface And ActionSupport implements the Validateable interface if (action instanceof Validateable & programmatic) {// The default value of programmatic is true Exception exception = null; // forcibly convert Validateable validateable = (Validateable) action; if (LOG. isDebugEnabled () {LOG. debug ("Invoking validate () on action" + validateable);} try {// call the method prefixed with validate and validateDo prefix PrefixMethodInvocationUtil. invokePrefixMethod (invocation, new String [] {VALIDATE_PR EFIX, ALT_VALIDATE_PREFIX});} catch (Exception e) {// If any exception occurred while doing reflection, we want // validate () to be executed LOG. warn ("an exception occured while executing the prefix method", e); exception = e;} // alwaysInvokeValidate defaults to true, always calling the validate method of Action if (alwaysInvokeValidate) {validateable. validate ();} if (exception! = Null) {// rethrow if something is wrong while doing validateXXX/validateDoXXX throw exception ;}} because struts2 provides the declarative verification function, the XML file is used to verify the submitted data. This declarative verification is performed by actionValidatorManager. validate (action, context); this code calls the validate method of ActionValidatorManager. The internal method is to find the corresponding XML validation file and parse the XML validation file to generate com. opensymphony. xwork2.validator. the Validator object, and then the data submitted by the object is verified. If some data is not validated, the corresponding error information is added to the field error information through addFieldError and printed to the console. Because the declarative verification function involves searching for XML verification files, parsing the XML verification file to generate the validator object, and then using the validator object to verify the data. The validator cache is also added, therefore, there is a large amount of code in it. here we can only talk about the general principle. If you are interested in the details, you can study it on your own. If Action inherits from the ActionSupport class (normally), The Validateable interface is implemented. Next, the verification method with the validate or validateDo prefix is called through the PrefixMethodInvocationUtil tool class, we have already met this tool class. when explaining the PrepareInterceptor interceptor, we will call methods with prepare or prepareDo prefixes. If a verification method with the prefix of validate or validateDo exists at the same time, only the method with the prefix of validate will be executed. This is determined by the internal code of the PrefixMethodInvocationUtil tool class. The alwaysInvokeValidate attribute of ValidationInterceptor is true by default, so the validate method of Action is always called, that is, validateable. validate (); this code will be executed. Use the code for verification in the validate method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.