一、拦截器的工作原理
拦截器的实现非常类似过滤器,它的工作过程是一个递归的过程。流程如下
request -->interceptor 1 ----> interceptor 2 ---->Action--->result---->interceptor2----->interceptor1--->response;
二、自定义拦截器
实现自定义拦截器有两种方法,一种是实现Interceptor接口,另一种是继承AbstractInterceptor类
这里具体讲解继承AbstractInterceptor类
1、定义一个类,继承AbstractInterceptor类。
2、实现该类的 String intercept(ActionInvocation ai) throws Exception方法。
ActionInvocation的作用,获取Action的执行状态,返回result字符串作为逻辑视图。
//执行下一个拦截器,或者执行最终Action,获取返回视图。通过执行String result=ai.invoke()实现
3、struts.xml中进行interceptor的配置
<interceptors>
<interceptor name="" class="">
Action 使用拦截器
<action>
<interceptor-ref name="拦截器名"/>