Struts. xml file
<? XML version ="1.0"Encoding =UTF-8"?>
<! Doctype struts public
"-// Apache Software Foundation // DTD strutsconfiguration 2.3 // en"
Http://struts.apache.org/dtds/struts-2.3.dtd>
<Struts>
<Package name ="Inter"Extends ="Struts-Default"Namespace ="/">
<! -- Loading declaration of interceptor -->
<Interceptors>
<! -- Declare a custom interceptor -->
<Interceptor name ="Mytimer"Class ="Cn. csdn. Time. Inter. mytimerinter"/>
<! -- Declare a custom interceptor Stack -->
<Interceptor-stack name ="Mystack">
<! -- Place the declared custom interceptor in the Custom interceptor Stack -->
<Interceptor-ref name ="Mytimer"/>
<! -- Place the interceptor stack in struts2 into the custom interceptor Stack -->
<Interceptor-ref name ="Defaultstack"/>
</Interceptor-stack>
</Interceptors>
<! -- Declare the default custom interceptor Stack -->
<Default-interceptor-ref name ="Mystack"/>
<Action name ="Mytimer"Class ="Cn. csdn. Time. Inter. mytimerinteraction"Method ="Say">
<Result>./mytimerinter. jsp </result>
</Action>
</Package>
</Struts>
Customize a time interceptor
Public class Mytimerinter ImplementsInterceptor {
Public voidDestroy (){
//TodoAuto-generatedmethod stub
System.Out. Println ("Custom time interceptor ------- execute at destruction .........");
}
Public voidInit (){
//TodoAuto-generatedmethod stub
System.Out. Println ("Custom time interceptor ------- execute at initialization .........");
}
PublicString intercept (actioninvocation Invocation)
ThrowsException {
//TodoAuto-generatedmethod stub
// Get the currently processed action. An object is returned.
ObjectOBJ= Invocation. getaction ();
// Define a time Parameter
LongTime = system.Currenttimemillis(); // Get the current number of milliseconds
System.Out. Println ("before the target method is executed ======= ");
String result = invocation. Invoke (); // call the system's invoke () method
System.Out. Println ("after the target method is executed =========" + (system.Currenttimemillis()-Time ));
ReturnResult;
}
}
The say () method in mytimerinteraction. Class
PublicString say (){
System.Out. Println ("I am the action method! ");
Return Success;
}
Page to jumpMytimerinter. jsp
<H1> I am a custom time interceptorJSPPage