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.