1.AOP:
AOP面向切面编程是OOP面向对象编程的完善补充。
2.拦截器:让一些通用的类可以插拔,从而更大的程度上解耦。进一步减少重复代码的书写(DRY:Don't Repeat Yourself)
3.拦截器工作原理
4.配置拦截器
1.可以在struts2-core-2.1.8.jar中的struts-default.xml文件中看到很多定义的拦截器
<interceptors>
定义拦截器
<interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
<interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>
定义拦截器栈(多个拦截器合并到一起组成一个拦截器栈)
<interceptor-stack name="basicStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
</interceptor-stack>
</interceptors>
5.拦截器和拦截器栈传参数很简单,
6.使用拦截器:<interceptor-ref name="拦截器名或者拦截器栈名"
注意:一旦为Action指定了某个拦截器,那么默认拦截器就不再起作用,要使用就必须手动配置。
5.自定义拦截器
1.要是实现一个拦截器类就必须实现Interceptor接口,要是实现接口中的三个方法(查看源码就知道了)
但是为了实现的方便,struts又提供了AbstractInterceptor抽象类,这样每次就只需要实现intercept方法就可以了
2.用户登录拦截器实例:
1.ActionInvocation对象是通过框架传过来的,通过ActionInvocation对象就可以获得session和 Action等对象。
3.文字过滤拦截器实例:
注意这里的拦截器的引用顺序不能反,因replace替换的类容必须是先由defaultStack的相关类容获得,不然后 就会报空指针。
<interceptor-ref name="defaultStack" />
<interceptor-ref name="replace" />
4.拦截器对方法的过滤(继承MethodFilterInterceptor)(P43)
<interceptor-ref name="methodInterceptor">
<param name="excludeMethods">execute1,execute2,execute3</param>//指定不被拦截的方法, //includeMethods指定要被拦截的方法。
</interceptor-ref>
5.拦截器的拦截顺序:
在Action的execute方法执行前,是按顺序来,但是在execute方法执行之后就是反顺序执行。
6.覆盖拦截器中的参数
7.内建拦截器