Struts2 Advanced "6"--Interceptor

Source: Internet
Author: User

Pee Interceptor

It can be said that the "interceptor" is the key content of Struts2. See the name of the meaning, the role of interceptors is mainly to intercept things, intercept what? Of course ' action ', the interceptor works before executing ' action ', executes some pre-processed code, then executes the associated method in the action, and then the process goes back inside the interceptor and then performs some subsequent operations.

There are also two points to be mentioned here:

1. Struts2 interceptors are pluggable, and interceptors are an implementation of AOP.
2. Interceptor Stack (interceptor stack). The Struts2 interceptor Stack is a chain that binds interceptors in a certain order. When accessing an intercepted method or field, interceptors in the Struts2 interceptor chain are invoked in the order in which they were previously defined.



implementing the Struts2 Interceptor principleThe block implementation of Struts 2 is relatively straightforward. When the request arrives at Struts2 's Servletdispatcher, Struts 2 looks for the configuration file, instantiates the relative interceptor object based on its configuration, and then strings it into a list, the last one to invoke the interceptor in the list. In fact, the reason we are able to use interceptors so flexibly is due to the use of dynamic agents. A dynamic agent is a proxy object that makes different processing based on the customer's needs. For the customer, just know a proxy object on the line.
In that Struts2, how is the interceptor invoked through a dynamic proxy? When the action request arrives, an action proxy object is generated by the agent of the system, which invokes the action's execute () or the specified method, and finds the interceptor corresponding to the action in Struts.xml. If there is a corresponding interceptor, these interceptors are called before the action's method executes, and if there is no corresponding interceptor, the action method is executed. The system's call to the Interceptor is implemented by Actioninvocation.

Look at a brief picture.


The interceptor works like every action request is wrapped inside a series of interceptors. The interceptor can do similar operations in the action execution line, or it can be recycled after the action is executed straight. Each action can either transfer the action to the following interceptor, or the action can exit the operation and return to the customer's given screen.



Code Examples

1. Writing interceptors
(1) Implement Interface Com.opensymphony.xwork2.Intercepter (or inherit com.opensymphony.xwork2.AbstractInterceptor)
(2) Add the following code to the Interceptor method:

Public String Intercept (Actioninvocation arg0) throws Exception {   System.out.println ("before");   Call   String result = Arg0.invoke () before action;  If there is an interceptor after this interceptor, call the Intercept method of the next interceptor                                   //If there is no interceptor, then invoke the Execute method of the action   System.out.println ("after");    return result;   }

2. Configuring Interceptors in Struts.xml
(1) in Struts.xml the Interceptor and interceptor stack are declared, and the interceptor stack can include multiple interceptors and other stacks.
<interceptors><!--interceptors--><interceptor name= "Myinterceptor" class= " Com.test.interceptor.MyInterceptor "></interceptor><!--Interceptor Stack--><interceptor-stack name=" Validationworkflowstack "><interceptor-ref name=" Basicstack "/>                <interceptor-ref name=" Validation " />                <interceptor-ref name= "Workflow"/></interceptor-stack></interceptors>

(2) Configure the Interceptor in a single action to intercept only the Execute method in this action.
<action name= "register" class= "com.test.action.RegisterAction" method= "test" ><result name= "Success" >/ Success.jsp</result><result name= "Input" >/register2.jsp</result><interceptor-ref name= " Myinterceptor "></interceptor-ref></action>

(3) Configure the Interceptor to all actions to intercept the Execute method in all action.
<default-interceptor-ref name= "Myinterceptor" ></default-interceptor-ref>
does not work for an action that has an interceptor configured individually

3. Intercept the method specified in the action
(1) Inherit com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.
(2) because it is a method for an action, it can only be configured inside the action
<action name= "register" class= "com.test.action.RegisterAction" method= "test" ><result name= "Success" >/ Success.jsp</result><result name= "Input" >/register2.jsp</result><interceptor-ref name= " Myinterceptor ">   <param name=" Includemethod ">test,execute</param> <!--intercept the text and execute methods, Method separated by commas--   <param name= "Excludemethod" >myfun</param>        <!--do not intercept Myfun method--></ Interceptor-ref></action>

4. In the Interceptor method of the Struts2 interceptor, the parameter actioninvocation can be used to obtain the information entered by the page user.

Public String Intercept (Actioninvocation arg0) throws Exception {    map map = Arg0.getinvocationcontext (). GetSession ( );    if (Map.get ("user") = = null) {        return action.login;    } else {        return arg0.invoke ();}    }


anyway

The Struts2 interceptor is the same as a servlet filter or JDK proxy class. Interceptors allow cross-cutting functions to be implemented separately, as well as frames.


Struts2 Advanced "6"--Interceptor

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.