On Struts2 interceptor

Interceptor:

      Before turning to the interceptor, I feel the need to mention the difference and filters.

      1. First Struts2 interceptor is their own, and only with the Struts2 framework can use interceptors, and the filter is part of the servlet specification, any java web project can be used;

           2. Second, the interceptor can intercept only Action, and the filter is theoretically possible to intercept anything ( JSP, HTML, CSS, Image or js etc. ).

      Interceptors arranged (in the Struts2.xml):

1  < Package Penalty for name = "default" the extends = "Struts-default" > 
2             .......
 3  <-! First declare a blocker, just the name, the class name is written in your own interceptor class path -> 
. 4      < interceptors > 
. 5              < Interceptor name = "priceInterceptor" class = "interceptor.priceInterceptor" > </ Interceptor > 
. 6       </ interceptors > 
. 7             .......
 . 8  <-! and then a use this action interceptor -> 
9  <action name= "updateBook" class = "action.BookAction" Method, = "Update" > 
10  <-! only to call their own definition of interceptors, the system default interceptors also need to manually call -> 
11              < Interceptor-ref name = "defaultStack" > </ Interceptor-REF > 
12 is              < Interceptor-REF name = "priceInterceptor" > </ Interceptor-REF > 
13 is              < Result name = "Success" > /updateBook.jsp </ Result > 
14              <result name="error">/updateBook.jsp</result>
15 </action>
16 </package>

 

When there is a request the Action, interceptor chain (is stored in a series of interceptor) will intercept interception process is: sequentially performing interception, every interceptor interceptor will find the corresponding class, and examples of this class, then the class execution intercept method (this method is stored inside a logic interception), if adopted, it will execute the next interceptor, the interceptor through the whole chain until the last action will be performed the method.

 1 return arg0.invoke(); 

Above this line is the current through the interceptor, interceptor to perform the next code (in the intercept method).

Why should mention here the following line: Because if you do not define interceptors, he is not without interceptors, and also there is a system of interceptor default, when you do not own definition of interceptors, these default interceptors automatically invoked when you request, but when you define interceptors, and call interceptor in this one action, then this action does not automatically call the system default interceptor, you need to manually call.

 

1 <interceptor-ref name="defaultStack"></interceptor-ref>

 

The following is the default interceptor down my project :( Interceptor-ref those that default interceptors, needs to be called)

 

 1 <interceptor-stack name="defaultStack">
 2                 <interceptor-ref name="exception"/>
 3                 <interceptor-ref name="alias"/>
 4                 <interceptor-ref name="servletConfig"/>
 5                 <interceptor-ref name="i18n"/>
 6                 <interceptor-ref name="prepare"/>
 7                 <interceptor-ref name="chain"/>
 8                 <interceptor-ref name="scopedModelDriven"/>
 9                 <interceptor-ref name="modelDriven"/>
10                 <interceptor-ref name="fileUpload"/>
11                 <interceptor-ref name="checkbox"/>
12                 <interceptor-ref name="multiselect"/>
13                 <interceptor-ref name="staticParams"/>
14                 <interceptor-ref name="actionMappingParams"/>
15                 <interceptor-ref name="params">
16                   <param name="excludeParams">dojo\..*,^struts\..*</param>
17                 </interceptor-ref>
18                 <interceptor-ref name="conversionError"/>
19                 <interceptor-ref name="validation">
20                     <param name="excludeMethods">input,back,cancel,browse</param>
21                 </interceptor-ref>
22                 <interceptor-ref name="workflow">
23                     <param name="excludeMethods">input,back,cancel,browse</param>
24                 </interceptor-ref>
25                 <interceptor-ref name="debugging"/>
26             </interceptor-stack>

 

        Well, the interceptor configuration finished, let's look at three kinds of ways to create the interceptor :( might mention this earlier part of the configuration of the interceptor speak better)

        Three kinds of ways to create here is to prepare the corresponding method interceptor class 3.

       1. Implement the Interceptor Interceptor Interface

    The interface has three methods init, intercept, destory, we just need our written intercept intercept logic in the herd;

  2. inherited abstract class AbstractInterceptor

    We just need to rewrite the intercept on the line, because AbstractInterceptor help us achieve the init (), destory () method Interceptor interface;

  3. inherit the abstract class MethodFilterInterceptor

    We need to rewrite doIntercept方法,this is our usual way (we also recommend in this way to create), because it has a way to create great advantage is the way in blocking action can be specified (the popular talk is the intercept interception, the interception should not intercept), the following example:

 

. 1  < Action name = "updateBook" class = "action.BookAction" Method = "Update" > 
2          < Interceptor-REF name = "defaultStack" > </ Interceptor-REF > 
. 3          < Interceptor-REF name = "priceInterceptor" > 
. 4              <! - add here does not intercept method -> 
. 5              < param name = "the random 1" > method name 1 </ param > 
. 6              < param name = "The free 2 ' > method name 2 </param>
 7          </interceptor-ref>
 8          <result name="success">/updateBook.jsp</result>
 9          <result name="error">/updateBook.jsp</result>
10 </action>

 

 

Configuration and create interceptors are finished, I hope for your help!

 

 

 

Guess you like

Origin www.cnblogs.com/baikaizhuliangshui/p/11622111.html