One, overview This article is mainly about how to customize the interceptor and through the custom interceptor to understand the interceptor lifecycle. Two, custom Interceptor 1) write an Interceptor class Hellointerceptor, which implements the Interceptor interface.
Package cn.bighuan.a_interceptor;
Import com.opensymphony.xwork2.ActionInvocation;
Import Com.opensymphony.xwork2.interceptor.Interceptor;
/** *
Custom Interceptor
* *
* @author Bighuan * */Public
class Hellointerceptor implements interceptor {
//Startup execution Public
Hellointerceptor () {
System.out.println ("Create Interceptor Object ...");
}
@Override public
void init () {System.out.println () (
"Hellointerceptor.init (): Executing Interceptor Initialization Method") is performed at startup ;
}
Interceptor Business Processing Method: (performed at the time of the Access action). Executed prior to execute. )
@Override public
String intercept (actioninvocation invocation) throws Exception {
System.out.println ("2, before executing action");
Call the next interceptor or action: equivalent to the Chain.dofilter (...)
in the servlet filter. Gets the return value of the Execute method
String Resultflag = Invocation.invoke ();
System.out.println ("4, Interceptor, Business processing-end:" + Resultflag);
return resultflag;
return null;
}
@Override public
Void Destroy () {
System.out.println ("Hellointerceptor.destroy (): Interceptor destroyed ...");
}
2) Write a actio class helloaction.
Package cn.bighuan.a_interceptor;
Import Com.opensymphony.xwork2.ActionSupport;
public class Helloaction extends Actionsupport {public
helloaction () {
System.out.println ("1, Helloaction.helloaction (): helloaction instance created ");
}
@Override public
String execute () throws Exception {
System.out.println ("3,helloaction.execute (): method to perform request processing ");
return Super.execute ();
}
3) in the Struts.xml file configuration, mainly pay attention to the interceptor configuration. (step in the code below)
<package name= "Hello_interceptor" extends= "Struts-default" > <!--Interceptor configuration--> <interceptors> & lt;! --Configure user-defined interceptors--> <interceptor name= "Hellointerceptor class=" Cn.bighuan.a_interceptor. Hellointerceptor "> </interceptor> <!--custom stack: To reference the default stack, otherwise the default stack function is gone, and the custom interceptor--> <interceptor-stack Name= "Hellostack" > <!--reference the default stack, note: The default stack must be placed on the first line--> <interceptor-ref name= "Defaultstack" ></interce
Ptor-ref> <!--referencing a custom interceptor--> <interceptor-ref name= "Hellointerceptor" ></interceptor-ref> </interceptor-stack> </interceptors> <!--execute all interceptors in the--> <default-interceptor-ref name= "hell Ostack "></default-interceptor-ref> <!--action configuration--> <action name=" Hello "class=" Cn.bighuan.a_inte Rceptor. Helloaction "> <result name=" Success ">/index.jsp</result> </action> </package>
4 Start the server and access the action. A when the server is just started, the console outputs the following:
The Interceptor object was created ...
Hellointerceptor.init (): The initialization method of the Interceptor was performed
b When the action is accessed, the console outputs the following:
1,helloaction.helloaction (): helloaction instance created
2, before executing action
3,helloaction.execute (): Method 4 executing request processing
, Interceptor , Business process-end: Success
D when the server is down, the console output is as follows:
Hellointerceptor.destroy (): Interceptor destroyed ...
Three, analysis of the Interceptor life Cycle 1 by the above experimental results, when we start the server, we have created an object that customizes all interceptors in the stack (if you do not refer to the custom interceptor stack, execute the default interceptor stack), and all objects perform initialization method init (). 2 When the user accesses the action, the first is to create the action example, not immediately executing the action's corresponding business logic method execute (), but executing the code before Invocation.invoke () in the business process method in the Interceptor ( Include yourself in this line of code. Invocation.invoke () is equivalent to Chain.dofilter () in the core business method of the filter in Servlet programming. The next thing to do is to execute the business logic method in the action execute (), After execution () is executed, the code following the Invocation.invoke () in the interceptor business process is continued until the method ends. 3 When the server is closed, the interceptor will destroy four, notice next blog is through the interceptor to achieve a complete login verification, please look forward to it!