Struts2 Interceptor (4): define your own interceptor by inheriting the abstractinterceptor abstract class

Source: Internet
Author: User

Abstract classAbstractinterceptorImplements the interceptor interface and provides empty implementations of the init and destroy methods. If our interceptor does not need to open resources, you do not need to implement these two methods. It can be seen that it is easier to implement a custom interceptor by inheriting the abstractinterceptor abstract class.

In the previous articleSimpleinterceptor. JavaChange to the following implementation, and the rest of the code will remain unchanged:

Public class simpleinterceptor extends actinterceptor {private string name; Public void setname (string name) {This. name = Name ;}@ overridepublic string intercept (actioninvocation Invocation) throws exception {loginaction = (loginaction) invocation. getaction (); system. out. println (name + "Interceptor action ------" + "Start the logon action:" + new date (); long start = system. currenttimemillis (); string result = invocation. invoke (); // understand this line of code system. out. println (name + "Interceptor action ------" + "the time when the logon action is executed is:" + new date (); long end = system. currenttimemillis (); system. out. println ("Time consumed to execute this action:" + (end-Start) + "millisecond"); return result; // understand this line of code }}

The struts2 interceptor is very powerful. It can insert Execution Code before the action's execute method, or insert Execution Code after the execute method. The essence of this method isAOP(Aspect-Oriented Programming.

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.