The role of interceptors:
The interceptor can dynamically intercept requests sent to the specified action, and through the interceptor mechanism, you can insert some code before and after the action execution to implant our logical thinking.
The implementation principle of interceptors:
Interceptors are invoked by proxy, by dynamically acting on our action, by inserting our interception code before and after the action is executed. The idea of this interceptor is an AOP that gets the facets of the business process, on a particular plane
Specific methods are automatically inserted through the system.
Interceptors in the STRUTS2:
In an ordinary project, we often need to parse the file fields in the upload form to prevent the form from being submitted multiple times ..., these actions are not actions that all action needs to implement and need to be freely combined in a dynamic manner. The Interceptor strategy is
Good solution to this problem, the Interceptor method is executed automatically before or after the target method executes, which completes the dynamic insertion of the common operation.
Interceptor Configuration:
Define an Interceptor:
class = "Interceptor Implementation class" > <param name= "parameter name" > Parameter value </param></interceptor>
You can also define an interceptor stack, which contains multiple interceptors.
<interceptor-stack name= "Interceptor stack name" > <interceptor-ref name= "Interceptor One"/> <interceptor-ref name= " Interceptor Two "/></interceptor-stack>
Custom interceptors:
To develop your own interceptor class, let's start by looking at how interceptor is.
Public Interface extends Serializable { void destroy (); void init (); throws Exception;}
As you can tell from the above code, the Init method initializes the class, and the Destroy method is to destroy the class, and the Intercept method is your actual interception. In addition, STRUTS2 provides us with a abstractinterceptor class that we can use to reduce the amount of code by inheriting this class, starting with the implementation of our own interceptors.
@SuppressWarnings ("Serial") Public classShowtimeinterceptorextendsAbstractinterceptor {PrivateString name; @Override PublicString Intercept (actioninvocation actioninvocation)throwsException {System.out.println ("I am" +name); SYSTEM.OUT.PRINTLN (Name+ " : " +NewDate ()); String result=Actioninvocation.invoke (); SYSTEM.OUT.PRINTLN (Name+ " : " +NewDate ()); returnresult; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; }}
then configure the information in the Struts.xml:
<Interceptors> <Interceptorname= "ShowTime"class= "Com.xujianguo.action.interceptor.ShowTimeInterceptor"> <paramname= "Name">Showtimeinterceptor</param> </Interceptor>
</Interceptors><Actionname= "Login"class= "Com.xujianguo.action.LoginAction"> <resultname= "Success">/login-success.html</result> <resultname= "Error">/login.html</result>
<!--default Interceptor Defaultstack--<Interceptor-refname= "Defaultstack"></Interceptor-ref> <Interceptor-refname= "ShowTime"></Interceptor-ref></Action>
Struts2 's Interceptor