Struts2 Study Notes --- custom interceptor

Source: Internet
Author: User

Struts2 Study Notes --- custom interceptor
What is an interceptor?

In struts2, interceptors are divided into the interceptors defined by Struts2 and custom interceptors. It intercepts an Action before it is executed, and adds some operations after the Action is executed.

Implementation Principle

When an Action is requested, struts2 searches for the configuration file, instantiates the corresponding interceptor object based on the Action configuration, Concatenates the object into a list, and finally calls the interceptor in the list one by one.

Interceptor Execution Process

1. pre-process the Action. (Execution in positive order)
2. the interceptor decides whether to execute the subsequent Interceptor (determined by the return value of the invoke () method ).
3. Post-process the Action. (Reverse execution)

Source code: Custom interceptor

Method 1: implement the Interceptor interface and rewrite the method.

Package org. test. interceptor; import com. opensymphony. xwork2.ActionInvocation; import com. opensymphony. xwork2.interceptor. interceptor; public class TestInterceptor1 implements Interceptor {// implement the Interceptor interface public TestInterceptor1 () {// constructor method. An interceptor instance System is generated when the server is started. out. println (TestInterceptor1 cons);} @ Override public void destroy () {// similar to the Destructor method, used to release the resource System. out. println (TestInterceptor1 destory);} @ Override public void init () {// the server is called at startup to initialize related resources, similar to the constructor System. out. println (TestInterceptor1 init) ;}@ Override public String intercept (ActionInvocation invocation) throws Exception {System. out. println (TestInterceptor1 intercept go); // start to execute this interceptor String resultName = invocation. invoke (); // execute the next interceptor or execute the Action's execute () method System. out. println (TestInterceptor1 intercept back); // return the interceptor System. out. println (TestInterceptor1 resultName: + resultName); // print the return value of the next interceptor or Action called: return resultName ;}}

Method 2: Inherit the MethodFilterInterceptor class and override the doIntercept () method.

Package org. test. interceptor; import com. opensymphony. xwork2.ActionInvocation; import com. opensymphony. xwork2.interceptor. abstractInterceptor; import com. opensymphony. xwork2.interceptor. interceptor; public class TestInterceptor2 extends actinterceptor {// inherit AbstractInterceptor, no need to Override init (), destroy () method @ Override public String intercept (ActionInvocation invocation) throws Exception {System. out. println (TestInterceptor2 intercept go); String resultName = invocation. invoke (); System. out. println (TestInterceptor2 intercept back); System. out. println (TestInterceptor2 resultName: + resultName); return resultName ;}}
Source code: Configure struts. xml

          
           
               
                
             
     
    
           
                       
    
     
/Success. jsp
                
    
     
/Error. jsp
                
                
                
             
     
                        
    
     
/Index. jsp
                
   
  
Source code: Definition of Action
public class TestAction extends ActionSupport {    @Override    public String execute() throws Exception {        System.out.println(TestAction execute);        return SUCCESS;    }}
Running result

1. the interceptor is called sequentially according to the order in which it is added to the action. Here TestInterceptor1 is called first, and then TestInterceptor2 is called.
2. The Action is executed only after all the interceptors are executed.
3. Then, back from the Action to the next call point, and then from TestAction to TestInterceptor2, from TestInterceptZ success? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> watermark + wLTSu9XFzbzA7b3iPGJyIC8 + DQo8aW1nIGFsdD0 =" here write picture description "src =" http://www.bkjia.com/uploads/allimg/150709/04303B5B-1.png "title =" "/>

 

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.