Struts2 18 Interceptor (19th)

Source: Internet
Author: User

The ultworkflowinterceptor interceptor is at the position of defaultStack 18th. It is the last interceptor in the defastack stack interceptor stack. Its main function is to determine whether the execution process of the current Action is incorrect, including ActionError and FieldError, for example, FieldError is added when an error occurs in request parameter type conversion. If an error occurs during execution, the Result is redirected to the specified Result (input by default ). This interceptor is inherited from the MethodFilterInterceptor class, so you can configure which methods should be applied to the interceptor and which methods should not be applied to the interceptor. below is the configuration of this interceptor in defaultStack: [html] <interceptor-ref name = "workflow"> <param name = "excludeMethods"> input, back, cancel, browse </param> </interceptor-ref> indicates that the input, back, cancel, and browse methods of the Action are not applied to the interceptor. The following is the source code of the doIntercept method of the Interceptor: [java] @ Override protected String doIntercept (ActionInvocation invocation) throws Exception {Object action = invocation. getAction (); // get the currently executed Action object // determine whether the Action implements ValidationAware. if (Action instanceof ValidationAware) is implemented by inheriting the action of the ActionSupport class) {ValidationAware validationAwareAction = (ValidationAware) action; // indicates whether an error occurs during Action execution. if (validationAwareAction. hasErr Ors () {if (LOG. isDebugEnabled () {LOG. debug ("Errors on action" + validationAwareAction + ", returning result name 'input'");} // The value of inputResultName is "input ", this is the String resultName = inputResultName written to the interceptor; // whether the Action has implemented the ValidationWorkflowAware interface if (action instanceof ValidationWorkflowAware) {// if yes, the getInputResultName () of Aciton () method determines the value of resultName = (ValidationWorkflowAware) action ). getI NputResultName ();} // determines whether the InputConfig annotation = Action is marked on the method to be executed by the current action. getClass (). getMethod (invocation. getProxy (). getMethod (), EMPTY_CLASS_ARRAY ). getAnnotation (InputConfig. class); if (annotation! = Null) {if (! Annotation. methodName (). equals ("") {// if the length of the methodName attribute of the InputConfig annotation is not 0 Method method = action. getClass (). getMethod (annotation. methodName (); // obtain the method resultName = (String) method specified by the methodName attribute of InputConfig annotation in Action. invoke (action); // then resultName is determined by the return value of this method} else {// otherwise, resultName = annotation is determined by the resultName attribute value of the InputConfig annotation. resultName () ;}/// return the resultName value to return resultName ;}// if the Action does not implement Validati The onAware interface directly executes the return invocation method in the Action. invoke ();} after learning struts2, I must know that if an error occurs in the Action Execution Process (the premise is to add it to ActionError or FieldError), it will turn to a Result named input, it is actually implemented through the interceptor, which is why the Result name is input. Of course, the name of this Result can be changed, and there are many ways: 1. pass the inputResultName parameter to the interceptor in the configuration file. The value of this parameter is determined by yourself. let Aciton implement the ValidationWorkflowAware interface. The Result name is determined by the getInputResultName method declared in the ValidationWorkflowAware interface. 3. add the InputConfig annotation to the method executed in the Action. The annotation has two attributes: methodName --> used to specify a method name in the current Action and use the return value of the method as the Result name; resultName --> directly specify the Result name. Finally, call invocation. invoke (); to execute the method in Action, because this is the last interceptor in defaultStack, so the method in Action is called ......

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.