Three configuration methods of Struts2 Interceptor Interceptor
Method 1. General Configuration Method
<struts> < PackageName= "Struts2"extends= "Struts-default" > <interceptors> <interceptor name= "Myinterceptor"class= "Edu.zd.core.MyInterceptor" ></interceptor> </interceptors> <action name= "register"class= "Edu.zd.action.RegisterAction" > <result name= "input" >/register.jsp</result> <re Sult>/result.jsp</result> <!--When you customize the interceptor and ref it, the system overrides the default Interceptor-stack (def Aultstack), in order to ensure that the system default Defaultstack is not impressed, we need to explicitly introduce it-<!--note the order of two interceptor-ref, the order is different, the execution effect is different: first configured first Perform/post-configuration exit first (advanced-out)-<interceptor-ref name= "Defaultstack" ></interceptor-ref> <in Terceptor-ref name= "Myinterceptor" ></interceptor-ref> </action> </ Package> </struts>
Method 2. Configure the Interceptor stack (an element that will concatenate multiple interceptor). The interceptor stack can then be introduced in <action>.
<struts> < PackageName= "Struts2"extends= "Struts-default" > <interceptors> <interceptor name= "Myinterceptor"class= "Edu.zd.interceptor.MyInterceptor" ></interceptor> <interceptor-stack name= "Myinterceptor Stack "> <interceptor-ref name=" myinterceptor "></interceptor-ref> <interc Eptor-ref name= "Defaultstack" ></interceptor-ref> </interceptor-stack> </interceptors > <action name= "Register"class= "Edu.zd.action.RegisterAction" > <result name= "input" >/register.jsp</result> <re sult>/result.jsp</result> <interceptor-ref name= "Myinterceptorstack" ></intercep Tor-ref> </action> </ Package> </struts>
Method 3. Modify the default interceptor to define the custom interceptor stack as the default interceptor for STRUTS2.
<struts> < PackageName= "Struts2"extends= "Struts-default" > <interceptors> <interceptor name= "Myinterceptor"class= "Edu.zd.interceptor.MyInterceptor" ></interceptor> <interceptor-stack name= "Myinterceptorstack" ; <interceptor-ref name= "Myinterceptor" ></interceptor-ref> <interceptor-ref name= "Defaultstac K "></interceptor-ref> </interceptor-stack> </interceptors> <!--this default inte Rceptor is for all actions--<!--If Interceptor is introduced into an action, this default interceptor is invalidated in this action--<default-interceptor-ref name= "Myinterceptorstack" ></default-interceptor-ref> <action name= "register"class= "Edu.zd.action.RegisterAction" > <result name= "input" >/register.jsp</result> <re Sult>/result.jsp</result> </action> </ Package> </struts>
2. Interceptor's Role object
(1) Intercept the target object (the object being proxied), where the target object is action;
(2) Interceptors (a class that dynamically inserts certain methods into the target object's before, after);
(3) The (dynamic) proxy object generated by the target object (the proxy object internal method synthesizes the target object method + Interceptor method). The program eventually executes the proxy for the target object, and the agent is already plugged into the interceptor.
Interceptor action: intercept action. The interceptor is equivalent to an entry and exit, enters the action by interceptor, executes the code of the action and then goes out through interceptor.
For the Myinterceptor.class and Myinterceptor2.class of "struts2-Interceptor (Interceptor How to write)" this article. Execute the program according to the following configuration file
<struts> < PackageName= "Struts2"extends= "Struts-default" > <interceptors> <interceptor name= "Myinterceptor"class= "Edu.hust.interceptor.MyInterceptor" ></interceptor> <interceptor name= "MyInterceptor2"class= "Edu.hust.interceptor.MyInterceptor2" ></interceptor> <interceptor-stack name= "Myinterceptorstack" > <interceptor-ref name= "myinterceptor" ></interceptor-ref> <interceptor- Ref name= "MyInterceptor2" ></interceptor-ref> <interceptor-ref name= "Defaultstack" ></inte Rceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name= "Myinterceptorstack" ></default-interceptor-ref> <action name= "register"class= "Edu.hust.action.RegisterAction" > <result name= "input" >/register.jsp</result> < Result>/result.jsp</result> </action> </ Package> </struts>
The console will get the following printout
Intercept start
Intercept2 start
2008-9-19 19:42:06 com.opensymphony.xwork2.validator.ActionValidatorManagerFactory <clinit>
Info: Detected Annotationactionvalidatormanager, initializing it ...
INTERCEPT2 Finish
Intercept finish
The result explains that "interceptor is the equivalent of an entry and exit, interceptor into action, executes the Code of action and then goes out through interceptor" sentence.
3. How the extends Methodfilterinterceptor interceptor configures which methods should be intercepted and which methods should not be intercepted (for the configuration of method interception)
<struts> < PackageName= "Struts2"extends= "Struts-default" > <interceptors> <interceptor name= "MyInterceptor3"class= "Edu.hust.interceptor.MyInterceptor3" ></interceptor> </interceptors> <action N Ame= "Register"class= "Edu.hust.action.RegisterAction" method= "Queryall" > <result name= "Input" >/register.jsp</result> ; <result>/result.jsp</result> <!--MyInterceptor3 interceptors are only for the Queryall () method and the Insert () method in Registeraction Intercept, other methods not blocked-<interceptor-ref name= "MyInterceptor3" > <param name= "Includemeth ODS ">queryall, execute</param> </interceptor-ref> <interceptor-ref name=" default Stack "></interceptor-ref> </action> <action name=" register "class= "Edu.hust.action.RegisterAction" method= "Insert" > <result name= "Input" >/register.jsp</result> <result>/result.jsp</result> <interceptor-ref name= "MyInterceptor3" > <param name= "Includemethods" >queryall, insert</param> </interceptor-ref> &L T;interceptor-ref name= "Defaultstack" ></interceptor-ref> </action> <action Name = "Register"class= "Edu.hust.action.RegisterAction" method= "Update" > <result name= "Input" >/register.jsp</result> <result>/result.jsp</result> <interceptor-ref name= "MyInterceptor3" > <param name= "Includemethods" >queryall, insert</param> </interceptor-ref> &L T;interceptor-ref name= "Defaultstack" ></interceptor-ref> </action> </ Package> </struts>
Configuration of the Struts2 interceptor