Struts2 custom interceptor Development
Introduction
In the previous article has talked about the basic concepts of interceptor (http://blog.csdn.net/xlgen157387/article/details/45951163), below we implement a custom interceptor together.
Interceptor Interface
public interface Interceptor extends Serializable { /** * Called to let an interceptor clean up any resources it has allocated. */ void destroy(); /** * Called after an interceptor is created, but before any requests are processed using * {@link #intercept(com.opensymphony.xwork2.ActionInvocation) intercept} , giving * the Interceptor a chance to initialize any needed resources. */ void init(); /** * Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the * request by the {@link ActionInvocation} or to short-circuit the processing and just return a String return code. * * @param invocation the action invocation * @return the return code, either returned from {@link ActionInvocation#invoke()}, or from the interceptor itself. * @throws Exception any system-level error, as defined in {@link com.opensymphony.xwork2.Action#execute()}. */ String intercept(ActionInvocation invocation) throws Exception;}
It can be seen that only three methods need to be implemented in the interface.
Implement the Interceptor Interface
Public class MyInterceptor implements Interceptor {public void destroy () {System. out. println ("destroy () ----");} public void init () {System. out. println ("init () ----");} public String intercept (ActionInvocation invocation) throws Exception {System. out. println ("before the Action is executed"); String result = invocation. invoke (); System. out. println ("after Result execution"); return result ;}}
We need to configure in the struts. xml file:
test
/welcome.jsp
First declare a custom Interceptor:
Then reference in action:
The execution results can be imagined:
Before the Action is executed, account = test, password = test, submitFlag = login after the Result is executed