Interceptor concept:
Interceptor is used in AOP (Aspect-OrientedProgramming) to intercept a method or field before it is accessed, and then add some operations before or after it. Interception is an implementation policy of AOP.
In the Chinese Document of Webwork, the interceptor is the object for dynamically intercepting Action calls. It provides a mechanism for developers to define the code to be executed before and after an action is executed, or to prevent it from being executed before an action is executed. It also provides a way to extract reusable parts of an action.
Struts2 places its core functions in the interceptor for implementation, instead of being dispersed into the Action for implementation. This facilitates system decoupling and makes the implementation of functions similar to the assembly of personal computers and becomes pluggable, if a function is required, an interceptor is "inserted", and an interceptor is "pulled out" without a function. You can combine the Interceptor to provide additional functions for the Action without modifying the Action code.
How the interceptor works
For example:
Struts2 built-in interceptor
Custom timer interceptor 1: inherits Java code from the AbstractInterceptor abstract class
package action; import com.opensymphony.xwork2.ActionSupport; public classHelloAction extends ActionSupport { private String message; @Override public String execute() throws Exception { message="Hello"; return super.execute(); } public String getMessage() { return message; } public void setMessage(Stringmessage) { this.message = message; } }
Jsp code: ---- for testing only
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%> My JSP 'index.jsp' starting page ${message }
Struts. xml
Timer interceptor
/Index. jsp
HelloAction timer interceptor
Custom Interceptor-java code
Package util; import java. util. date; import com. opensymphony. xwork2.ActionInvocation; import com. opensymphony. xwork2.interceptor. abstractInterceptor; public classMyInterceptor extends actinterceptor {/*****/private static final long serialVersionUID = 1L; private String name; @ Override public Stringintercept (ActionInvocation invocation) throws Exception {System. out. println ("before calling" + new Date (); long start = System. currentTimeMillis (); Stringresult = invocation. invoke (); long end = System. currentTimeMillis (); System. out. println ("after calling" + new Date (); System. out. println (name + "time used" + (end-start); return result;} public void setName (String name) {this. name = name ;}}
Ii. inherit MethodFilterInterceptZ tokens? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcjwvaDI + CjxoMz5KYXZhtPrC6zwvaDM + incubator + IDwvcD4KPHByZSBjbGFzcz0 =" brush: java; "> public String add () {message = "add"; return SUCCESS;} public String delete () {message = "delete"; return SUCCESS ;}
Custom Interceptor-java code
Package util; import java. util. date; importcom. opensymphony. xwork2.ActionInvocation; importcom. opensymphony. xwork2.interceptor. methodFilterInterceptor; public class MethodInteceptor extends {/***/privatestatic final long serialVersionUID = 1L; privateString name; @ Override publicString doIntercept (ActionInvocation invocation) throws Exception {System. out. println (name + "before calling" + newDate (); longstart = System. currentTimeMillis (); Stringresult = invocation. invoke (); longend = System. currentTimeMillis (); System. out. println (name + "after calling" + newDate (); System. out. println (name + "time used" + (end-start); returnresult;} publicvoid setName (String name) {this. name = name ;}}
Struts. xml
Timer interceptor 1
Timer interceptor 2
/Index. jsp
HelloAction timer interceptor 1
HelloAction timer interceptor 2
Add, delete
Execute
:
Explanation:
MethodFilterInterceptor selects the interceptor or exclusion method by specifying the list of included/excluded methods. The following parameters can be set:
ExcludeMethods ------- the method to be excluded.
IncludeMethods -------- method to intercept
The add and delete methods are excluded from the struts. xml file in China, so they do not take effect. So the following situation occurs!
Access: http: /localhost: 8080/interceptor/hello/execute
Access http: // localhost: 8080/interceptor/hello/add