1. Struts2 Method Interceptor Overview
The Struts2 interceptor can also be implemented by the Methodfilterinterceptor class, Methodfilterinterceptor overrides the Intercept of the Abstractinterceptor class ( Actioninvocationinvocation) method, but provides a dointercept (Actioninvocation invocation) abstract method. From this design, it can be seen that the Methodfilterinterceptor class of intercept has achieved the action of the Interception behavior (just implement the method of filtering logic), but the real interception logic needs to be provided by the developer, That is, the callback Dointercept method is implemented. It can be seen that if the user needs to implement their own interception logic, the dointercept (actioninvocation invocation) method should be overridden.
There is not much difference between implementing a method-filtered interceptor and implementing an ordinary interceptor, except that there are only two places to be aware: the interceptor that implements the filter needs to inherit the Methodfilterinterceptor abstract class, and overrides the Dointercept method to define the intercept logic for the action.
In the Methodfilterinterceptor method, the following two additional methods are added:
public void Setexcludemethods (string excludemethods): Excludes methods that need to be filtered-the Set method "blacklist", and all methods listed in the Excludemethods string are not intercepted.
public void Setincludemethods (string includemethods): Sets the method to be filtered-the set method "Whitelist", all methods listed in the Includemethods string are intercepted.
Note: If a method is listed in both Excludemethods and Includemethods, the method is intercepted.
Because the Methodfilterinterceptor class contains the two methods above, the subclass of the Interceptor also obtains both methods. You can specify methods in the configuration file that need to be intercepted, or that do not need to be intercepted.
2, before the development of the preparatory work
To have a struts2 environment that can run
3. Implement the source code of the interceptor that inherits the Methodfilterinterceptor
PackageCom.struts2;ImportCom.opensymphony.xwork2.ActionInvocation;ImportCom.opensymphony.xwork2.interceptor.MethodFilterInterceptor;/** * Simple implementation of STRUTS2 interception method Interceptor, inheriting Methodfilterinterceptor abstract class * * @author Fan Fangming */ Public class easymethodinterceptor extends methodfilterinterceptor { //The name of the simple interceptor PrivateString name;//Override the Dointercept method to implement the intercept logic for the action PublicStringdointercept(Actioninvocation invocation)throwsException {LongStart = System.currenttimemillis ();//Execute the last interceptor of the interceptor, or specify the Execute method of the action directly //loginaction action = (loginaction) invocation.getaction ();String result = Invocation.invoke ();LongEnd = System.currenttimemillis (); SYSTEM.OUT.PRINTLN (name +", the time that this action was detected is:"+ (End-start) +"milliseconds");returnResult } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; }}
4, with the test action
PackageCom.struts2;ImportCom.opensymphony.xwork2.ActionSupport;/** * Simple implementation of action * @author Fan Fangming */ Public class loginaction extends actionsupport { Private Static Final LongSerialversionuid =7854497526623985504L//Master execution method PublicStringExecute()throwsException {System.out.println ("---loginaction is executed. ");return "Success"; }//Another method, this method should not be intercepted PublicStringGetexcludemethod()throwsException {System.out.println ("---getexcludemethod, I don't want to be intercepted. ");return "Success"; }}
5, with the test login.jsp
<%@ page language="java" import="java.util.*" pageencoding= "GBK"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" ><html><head><title>My JSP ' index.jsp ' starting page</title></head> <body> <form Action="Alogin.action" method="POST" name ="Form1"> <table Width="392" border="1"> <tr Align="center"> <TD colspan="2" bgcolor= "#FFCCFF"><input type="Submit" value="I will be intercepted and logged in" /> </td> </tr> </table> </form> <form Action="Getexcludemethod.action" method="POST" name ="Form1"> <table Width="392" border="1"> <tr Align="center"> <TD colspan="2" bgcolor= "#FFCCFF"><input type="Submit" value="I don't want to be intercepted" /> </td> </tr> </table> </form> </body></html>
6. Modification of STRUTS.XM
<package name= "First" extends= "Struts-default" ><!--Define a package--<interceptors> <interce Ptor name= "Myinterceptor" class= "Com.struts2.EasyMethodInterceptor" > <param name= "Name" > I'm Struts2 's way to stop Cutter </param> <param name= "Excludemethods" >getExcludeMethod</param> </interceptor& Gt <interceptor-stack name= "Myinterceptorstack" > <interceptor-ref name= "Myinterceptor"/> & Lt;interceptor-ref name= "Defaultstack"/> </interceptor-stack> </interceptors> <!--to action Returns the configuration of the results--<action name= "Alogin" class= "com.struts2.LoginAction" > <result name= "Success" >/index. jsp</result> <interceptor-ref name= "Myinterceptorstack" ></interceptor-ref> </action> <action name= "Getexcludemethod" class= "com.struts2.LoginAction" method= "Getexcludemethod" > <result name= "s Uccess ">/index.jsp</result> <interceptor-ref name= "Myinterceptorstack" ></interceptor-ref> </action> ;</package> The code above is specified by the Excludemethods property that the Execute method does not need to be intercepted, and if the browser sends a request again to the login action in the Explorer, no output will be visible in the Tomcat console , indicating that the interceptor did not intercept the action's Execute method. If you need to specify that multiple methods are not intercepted by the interceptor at the same time, multiple methods are separated by commas (,).
7. Operation Result
Start Middleware, enter http://127.0.0.1:8080/webStudy/login.jsp
See two commit buttons, click From top down, background output:
-loginaction is executed.
I'm Struts2 's method interceptor, which detects when this action is executed: 134 milliseconds
-getexcludemethod, I don't want to be intercepted.
A good memory is better than a bad writer. 50-javaweb method interceptors in frame struts2 (5)