Struts2-学习笔记系列(14)-拦截器
6.1对action 的拦截
自定义拦截器:
1 public class MyInterceptor extends AbstractInterceptor { 2 3 private String name; 4 5 6 @Override 7 public String intercept(ActionInvocation actionInvocation) throws Exception { 8 //获取被拦截的action实例 9 LoginAction action = (LoginAction) actionInvocation.getAction(); 10 System.out.print(name + " 拦截器开始执行:" + new Date()); 11 long start = System.currentTimeMillis(); 12 //执行该拦截器的下一个拦截器 13 //然后调用action 的执行方法 14 String result = actionInvocation.invoke(); 15 System.out.print(name + " action执行完成后:" + new Date()); 16 long end = System.currentTimeMillis(); 17 System.out.print("执行完该action的时间为:" + (end - start) + "毫秒"); 18 return result; 19 } 20 21 public void setName(String name) { 22 this.name = name; 23 } 24 } 25 26
若是需要在拦截器中实现init和destory方法,则需要实现interceptor
引用拦截器:
1 <package name="lee" extends="struts-default"> 2 <interceptors> 3 <interceptor name="zcx" class="interceptors.MyInterceptor"> 4 <param name="name"></param> 5 </interceptor> 6 </interceptors> 7 <action name="login" class="zcx.controller.LoginAction"> 8 <!-- 配置名为input的逻辑视图,当转换失败后转入该逻辑视图 --> 9 <result name="input">/WEB-INF/content/input.jsp</result> 10 <result>/WEB-INF/content/welcome.jsp</result> 11 <result name="error">/WEB-INF/content/welcome.jsp</result> 12 <interceptor-ref name="defaultStack"></interceptor-ref> 13 <interceptor-ref name="zcx"> 14 <param name="name">改名后的拦截器</param> 15 </interceptor-ref> 16 </action> 17 <action name="*"> 18 <result>/WEB-INF/content/{1}.jsp</result> 19 </action> 20 </package>
需要注意的是,当在struts文件中显示配置了拦截器,则默认的拦截器不生效。因此,需要在struts中先指定默认拦截器。若是遇到后面有同名拦截器,则会将默认的覆盖.
指定默认拦截器
<interceptor-ref name="defaultStack"></interceptor-ref>
6.2对方法的拦截
1 public class MyMethondInterceptor extends MethodFilterInterceptor { 2 private String name; 3 4 @Override 5 protected String doIntercept(ActionInvocation actionInvocation) throws Exception { 6 //获取被拦截的action实例 7 LoginAction action = (LoginAction) actionInvocation.getAction(); 8 System.out.print(name + " 拦截器开始执行:" + new Date()); 9 long start = System.currentTimeMillis(); 10 //执行该拦截器的下一个拦截器 11 //然后调用action 的执行方法 12 String result = actionInvocation.invoke(); 13 System.out.print(name + " action执行完成后:" + new Date()); 14 long end = System.currentTimeMillis(); 15 System.out.print("执行完该action的时间为:" + (end - start) + "毫秒"); 16 return result; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 }
1 <package name="lee" extends="struts-default"> 2 <interceptors> 3 <interceptor name="zcx" class="interceptors.MyMethondInterceptor"> 4 <param name="name">嘻嘻</param> 5 </interceptor> 6 </interceptors> 7 <action name="login" class="zcx.controller.LoginAction"> 8 <!-- 配置名为input的逻辑视图,当转换失败后转入该逻辑视图 --> 9 <result name="input">/WEB-INF/content/input.jsp</result> 10 <result>/WEB-INF/content/welcome.jsp</result> 11 <result name="error">/WEB-INF/content/welcome.jsp</result> 12 <interceptor-ref name="defaultStack"></interceptor-ref> 13 <interceptor-ref name="zcx"> 14 <param name="name">改名后的拦截器</param> 15 <!--指定execute不需要被拦截--> 16 <param name="excludeMethods">execute</param> 17 <param name="includeMethods">execute</param> 18 </interceptor-ref> 19 </action> 20 <action name="*"> 21 <result>/WEB-INF/content/{1}.jsp</result> 22 </action> 23 </package>
excludeMethonds:不拦截execute方法,后一个相反
<param name="excludeMethods">execute</param>
<param name="includeMethods">execute</param>
6.3拦截器执行顺序
在action控制方法执行之前,拦截器配置在前面那个先起作用;action控制方法执行之后,配置在后面的拦截器先起作用
6.4覆盖拦截器栈的参数
如果需要在使用拦截器栈时直接覆盖某个拦截器的属性值,则在指定需要被覆盖的属性时,不能只指定属性名,必须加上该属性属于的拦截器名:<拦截器>.<属性名>