struts2的interceptor的使用总结
1、体现AOP(面向切面编程)
2、在struts.xml配置文件的<action>内部调用
3、可以使用默认的,也可以使用自己写的,调用方法一样
<interceptor-ref name="defaultStack" />
<interceptor-ref name="myInterceptor"/>
4、默认的拦截器为struts2提供了基础服务,当声明自己的拦截器时必须显示声明默认的拦截器
5、自己的拦截器需要在struts.xml中配置
<interceptors>
<interceptor name="myInterceptor" class="demo.MyInterceptor" />
</interceptors>
6、可以将多个拦截器组成一个拦截器栈,引用方法和单个拦截器没有差异, <interceptor-ref name="otherDefaultInterceptor" />
<interceptors>
<interceptor name="myInterceptor" class="MyInterceptor" />
<interceptor name="otherMyInterceptor" class="OtherMyInterceptor" />
<!-- 已在spring的applicationContext-bean.xml中配置相关bean -->
<interceptor-stack name="defaultInterceptor">
<interceptor-ref name="myInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
<interceptor-stack name="otherDefaultInterceptor">
<interceptor-ref name="otherMyInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
7、多个action可以共享一个拦截器(栈),整个package都可以使用,当全局声明和单个action声明同时存在时,单个action的声明起作用
<default-interceptor-ref name="defaultInterceptor" />
8、当不声明任何拦截器的时候,默认的拦截器是发挥作用的
9、总之,默认拦截器在没有任何拦截器声明时起作用,任何显示声明的拦截器都会覆盖默认拦截器所以需要同时声明默认拦截器,全局声明拦截器与action内声明拦截器同时存在时action内声明的拦截器起作用。
10、编写自己的拦截器类时可以继承AbstractInterceptor,仅必须实现 intercept 方法,如需要可以重写其他两个方法,也可以继承Interceptor类,不过一般较少使用这个需要多写两个不怎么使用的方法。
参考文章:http://blog.youkuaiyun.com/linjingj/article/details/9622693
参考链接:http://www.blogjava.net/max/archive/2006/12/06/85925.html