07-拦截器
1 拦截器的创建
1 方式1-实现 Interceptor
package www.test.a_interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; //拦截器:第一种创建方式 //拦截器生命周期:随项目的启动而创建,随项目关闭而销毁 public class MyInterceptor implements Interceptor{ @Override //初始化方法 public void init() { } @Override //拦截方法 public String intercept(ActionInvocation arg0) throws Exception { return null; } @Override //销毁方法 public void destroy() { } }
2 方式 2-继承 AbstractInterceptor
package www.test.a_interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; //创建方式2: 继承AbstractInterceptor -> struts2的体贴 //帮我们空实现了init 和 destory方法. 我们如果不需要实现这两个方法,就可以只实现intercept方法 public class MyInterceptor2 extends AbstractInterceptor { @Override public String intercept(ActionInvocation arg0) throws Exception { return null; } }
3 方式 3-继承 MethodFilterInterceptor
package www.test.a_interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; //继承:MethodFilterInterceptor 方法过滤拦截器 //功能: 定制拦截器拦截的方法. // 定制哪些方法需要拦截. // 定制哪些方法不需要拦截 public class MyInterceptor3 extends MethodFilterInterceptor { @Override protected String doIntercept(ActionInvocation invocation) throws Exception { //前处理 System.out.println("MyInterceptor3的前处理"); //放行 String result = invocation.invoke(); //后处理 System.out.println("MyInterceptor3的后处理"); return result; } }
2 拦截器的配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true"></constant> <package name="interceptor" namespace="/" extends="struts-default"> <interceptors> <!--1 注册拦截器 --> <interceptor name="myInterceptor3" class="www.test.a_interceptor.MyInterceptor3"></interceptor> <!--2 注册拦截器栈 --> <interceptor-stack name="myStack"> <!--引入自定义的拦截器(建议放在20个默认拦截器之前) --> <interceptor-ref name="myInterceptor3"> <!--指定哪些方法不需要拦截 --> <!-- <param name="excludeMethods">add,find</param> --> <!--指定哪些方法需要拦截 --> <param name="includeMethods">find,add</param> </interceptor-ref> <!--引用默认的拦截器(20个) --> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <!--3指定包中的默认拦截器栈 --> <default-interceptor-ref name="myStack"></default-interceptor-ref> <action name="Demo1Action_*" class="www.test.a_interceptor.Demo1Action" method="{1}"> <!--为Action单独指定走那个拦截器栈 --> <!-- <interceptor-ref name="myStack"></interceptor-ref> --> <result name="success" type="dispatcher">/index.jsp</result> </action> </package> </struts>
3 其它
1 Demo1Action
package www.test.a_interceptor; import com.opensymphony.xwork2.ActionSupport; public class Demo1Action extends ActionSupport { public String add() throws Exception { System.out.println("Demo1Action_add!"); return SUCCESS; } public String delete() throws Exception { System.out.println("Demo1Action_delete!"); return SUCCESS; } public String update() throws Exception { System.out.println("Demo1Action_update!"); return SUCCESS; } public String find() throws Exception { System.out.println("Demo1Action_find!"); return SUCCESS; } }
2 拦截器api
MyInterceptor3中如果使用return "success"; 那么执行了MyInterceptor3这个拦截器之后就不会在执行下面的20个默认拦截器了。