In the previous article introduced the principle of the Struts2 interceptor, in this article we will learn how to write their own interceptor.
First, the implementation of the Interceptor
Implementing an interceptor is very simple. In fact, an interceptor is an ordinary class, except that the class must implement the Com.opensymphony.xwork2.interceptor.Interceptor interface. The Interceptor interface has the following three methods:
Public interface Interceptor extends Serializable
{
void Destroy ();
void Init ();
String Intercept (actioninvocation invocation) throws Exception;
}
Where the init and destroy methods are executed only once when the interceptor is loaded and disposed (both by the Struts2 itself). The Intercept method is invoked every time the action is accessed. Struts2 when calling interceptors, each interceptor class has only one object instance, and all actions referencing this interceptor share an object instance of this interceptor class, so if class variables are used in classes that implement the Interceptor interface, you should pay attention to synchronization problems.
Let's implement a simple interceptor that uses the request parameter action to specify a method in the Interceptor class and call this method (we can use the interceptor to preprocess a particular action). If the method does not exist, or if the action parameter does not exist, proceed to the following code. such as the following URL:
Http://localhost:8080/struts2/test/interceptor.action?action=test
When the above URL is accessed, the interceptor invokes the test method in the interceptor, if the method does not exist, Call the Invocation.invoke method, the Invoke method and the servlet filter call the Filterchain.dofilter method, and if there are other interceptors behind the current interceptor, the Invoke method is the Intercept method that calls the back interceptor, no , invoke invokes the Execute method (or other execution method) of the action class.
Let's implement a actioninterceptor of the Interceptor's parent class first. This class basically implements the ability to invoke a method based on the value of the action parameter, as follows:
Package Interceptor Import com.opensymphony.xwork2.ActionInvocation; import
Com.opensymphony.xwork2.interceptor.Interceptor;
Import javax.servlet.http.*;
Import org.apache.struts2.*;
public class Actioninterceptor implements interceptor {protected final String INVOKE = "# #invoke";
public void Destroy () {System.out.println ("destroy");
public void init () {System.out.println ("init"); Public String intercept (actioninvocation invocation) throws Exception {HttpServletRequest request =
Servletactioncontext.getrequest ();
String action = request.getparameter ("action");
System.out.println (This.hashcode ()); if (action!= null) {try {Java.lang.reflect.Method method = This.getclas
S (). GetMethod (action);
String result = (string) method.invoke (this); if (result!= null) {if (!result. Equals (INVOKE)) return result;
else return null;
catch (Exception e) {}} return Invocation.invoke (); }
}