1, Struts2 of the Interceptor overview
The Struts2 interceptor is intercepted before a method that accesses an action or action, or is processed after it has been run. Struts2 interceptors are pluggable, and interceptors are an implementation of AOP (Aspect oriented programming, aspect-oriented programming)
The Struts2 Interceptor Stack (interceptorstack) is a chain that binds interceptors in a certain order. When accessing an intercepted method or field, interceptors in the Struts2 interceptor chain are invoked in the order in which they were previously defined.
STRUTS2 specifies that a user-defined interceptor must implement the Com.opensymphony.xwork2.interceptor.Interceptor interface, which declares 3 methods
void Init ();
void Destroy ();
String Intercept (actioninvocation invocation) throws Exception;
The Init and destroy methods are executed once at the beginning and end of the program, regardless of whether the interceptor is used or not, as long as the STRUTS2 interceptor is declared in Struts.xml.
The Intercept method is the subject of the interception, and the logic is executed each time the interceptor comes into effect.
However, struts provides several abstract classes to simplify this step
Public abstract class Abstractinterceptor implements interceptor;
Public abstract class Methodfilterinterceptor extends Abstractinterceptor;
Are all implemented by the template method.
Where Abstractinterceptor provides an empty implementation of Init () and Destroy (), only The Intercept () method is required to be used;
Methodfilterinterceptor provides the Includemethods and excludemethods two properties to filter the action that executes the filter. You can use Param to add or exclude methods that require filtering.
2, before the development of the preparatory work
To have a struts2 environment that can run
3, directly implement the Interceptor Interface source code
PackageCom.struts2;ImportCom.opensymphony.xwork2.ActionInvocation;ImportCom.opensymphony.xwork2.interceptor.Interceptor;/** * Simple implementation of STRUTS2 Interceptor * @author Fan Fangming */ Public class easyinterceptor implements Interceptor { Public void Destroy() {System.out.println ("---interceptor destruction---"); } Public void Init() {System.out.println ("---Interceptor initialization---"); } PublicStringIntercept(Actioninvocation invocation)throwsException {System.out.println ("Insert Code before action executes");//Execute target method (call next interceptor, or execute action) FinalString res = Invocation.invoke (); System.out.println ("Insert Code after action execution");returnRes }}
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"; }}
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="Login" /> </td> </tr> </table> </form> </body></html>
6. Modification of STRUTS.XM
<package name="first" extends="struts-default"><!-- 定义一个package -->
<interceptors>
<interceptor name="MyInterceptor" class="com.struts2.EasyInterceptor"></interceptor>
<interceptor-stack name="myInterceptorStack">
<interceptor-ref name="MyInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!-- 对action返回结果的配置 -->
<action name="aLogin" class="com.struts2.LoginAction">
<result name="success">/index.jsp</result>
<interceptor-ref name="myInterceptorStack"></interceptor-ref>
</action>
</package>
7. Operation Result
Start Middleware, enter http://127.0.0.1:8080/webStudy/login.jsp
Click "Login" and the console prints out:
Insert Code before action executes
-loginaction is executed.
Insert Code after action executes
A good memory is better than a bad writer. 49-javaweb in frame struts2 (4)