Custom time interceptor

Source: Internet
Author: User

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

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.