struts 拦截器

来源:互联网 发布:朗威数字化实验室 淘宝 编辑:程序博客网 时间:2024/05/17 02:43

一、拦截器与过滤器的区别

1、拦截器是基于java的反射机制的,而过滤器是基于函数回调
2、过滤器依赖与servlet容器,而拦截器不依赖与servlet容器
3、拦截器只能对action请求起作用,而过滤器则可以对几乎所有的请求起作用
4、拦截器可以访问action上下文、值栈里的对象,而过滤器不能
5、在action的生命周期中,拦截器可以多次被调用,而过滤器只能在容器初始化时被调用一次

执行顺序:过滤前 – 拦截前 – Action处理 – 拦截后 – 过滤后

二、拦截器的概念

拦截器(Interceptor):是动态拦截Action调用的对象,类似于Servlet中的过滤器,在执行Action的业务逻辑处理方法(execute())之前,struts2会首先执行在struts.xml中引用的拦截器。
拦截器是struts2的一个重要特性,Struts2框架的大多数核心功能都是通过拦截器来实现的,像避免避免表单重复提交,类型转换,对象组装,验证,文件上传等,都是在拦截器的帮助下实现的。拦截器可以在Action执行之前和执行之后拦截调用。

谈到拦截器,还有一个词大家应该知道——拦截器链(Interceptor Chain,在Struts 2中称为拦截器栈Interceptor Stack)。拦截器链就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。


三、拦截器实现原理

大部分时候,拦截器方法都是通过代理的方式来调用的。Struts 2的拦截器实现相对简单。当请求到达Struts 2的ServletDispatcher时,Struts 2会查找配置文件,并根据其配置实例化相对的拦截器对象,然后串成一个列表(list),最后一个一个地调用列表中的拦截器。事实上,我们之所以能够如此灵活地使用拦截器,完全归功于“动态代理”的使用。动态代理是代理对象根据客户的需求做出不同的处理。对于客户来说,只要知道一个代理对象就行了。那Struts2中,拦截器是如何通过动态代理被调用的呢?当Action请求到来的时候,会由系统的代理生成一个Action的代理对象,由这个代理对象调用Action的execute()或指定的方法,并在struts.xml中查找与该Action对应的拦截器。如果有对应的拦截器,就在Action的方法执行前(后)调用这些拦截器;如果没有对应的拦截器则执行Action的方法。其中系统对于拦截器的调用,是通过ActionInvocation来实现的。代码如下:

if (interceptors.hasNext()) {     Interceptor interceptor=(Interceptor)interceptors.next();     resultCode = interceptor.intercept(this);     } else {     if (proxy.getConfig().getMethodName() == null) {     resultCode = getAction().execute();     } else {     resultCode = invokeAction(getAction(), proxy.getConfig());     }     }

可以发现Action并没有与拦截器发生直接关联,而完全是“代理”在组织Action与拦截器协同工作。如下图:

四、模拟Struts2实现一个拦截器系统

package lanjieqi.struts2;import java.util.LinkedList;import java.util.List;public class MyInvocation {private List<Interceptor> interceptors = new LinkedList<Interceptor>();// 保存拦截器对象private Object action;// 被拦截的对象private int interceptorIndex = 0;// 拦截器的调用索引// 构造方法,用于注册拦截器和Action 对象public MyInvocation(Object action, Interceptor... interceptor) {// 将拦截器对象加到interceptors中for (int i = 0; i < interceptor.length; i++) {// 将拦截器添加到List集合this.interceptors.add(interceptor[i]);}this.action = action;}// 执行调用链中的拦截器方法和execute方法public void invoke() throws Exception {// 调用链中的所有拦截器方法都执行完了,开始调用execute方法if (interceptorIndex == interceptors.size()) {try {// 通过反射技术在Action 对象中寻找execute方法如果未找到,将跑出异常java.lang.reflect.Method method = action.getClass().getMethod("execute");method.invoke(getAction());} catch (Exception e) {throw new Exception("在action中未发现execute方法");}return;}interceptors.get(interceptorIndex++).intercept(this);}// 获得Action对象public Object getAction() {return this.action;}}

五、自定义拦截器

1、自定义拦截器

第一种方式:
实现xwork包下的Interceptor接口
init() {} 初始化时调用
destroy() {} 销毁时调用
class MyInterceptor implements Interceptor { private String hello; //setter...getter... public void init() {  System.out.println("init()...");  System.out.println( hello ); } public void destroy() {  System.out.println("destroy()..."); } public String intercept(ActionInvocation invocation) throws Exception {  System.out.println("intercept()1...");  //拦截器有拦截器站  //invoke()将判断是否还有下一个拦截器,有就执行下一个拦截器,没有则开始执行被拦截的类  String result = invocation.invoke();  System.out.println("finish1...");  return result; }
第二种方式:

该类实现了Interceptor接口,并且空实现了init()和destroy()方法

继承AbstractInterceptor 
public class MyInterceptor extends AbstractInterceptor { public String intercept(ActionInvocation invocation) throws Exception {  System.out.println("intercept()2...");  String result = invocation.invoke();  System.out.println("finish2...");  return result; }}
2. struts.xml 配置

<struts> struts采用包名把不同的action区分开,相同类似功能的放在一个包里 包之间是可以有继承关系的, extends="struts-default" 相当于把struts2-core-2.0.11.jar下的struts-default.xml的东西给继承过来了 其中包括N多的拦截器  <package name="包名" extends="struts-default">    定义拦截器  <interceptors>   定义多个拦截器时可以包含多个<interceptor>   <interceptor name="myInterceptor" class="com....interceptor.MyInterceptor"/>   <interceptor name="myInterceptor2" class="...">    为拦截器配置参数    name对应到拦截器类中的字段属性,该属性需要有set、get方法    当启动拦截器时这项配置将会吧world作为值赋给私有成员hello    也可以在引用拦截器时改变这个值    <param name="hello">world</param>   </interceptor>  </interceptors>  声明拦截器栈  <interceptors-stack name="myInterceptorStack">   引用拦截器   <interceptor-ref name="myInterceptor" />   <interceptor-ref name="myInterceptor2" />   拦截器栈中可以包含拦截器栈   <interceptor-ref name="defaultStack" />   defaultStack---Struts2默认的拦截器     </interceptors-stack>  <interceptors-stack name="myInterceptorStack2">      <interceptor-ref name="myInterceptor" />   引用拦截器栈   <interceptor-ref name="myInterceptorStack" />       </interceptors-stack>  <action ...>   <result name="...">...</result>   ...   使用拦截器 action被请求一次,拦截器的intercept()就被执行一次   引用拦截器   <interceptor-ref name="myInterceptor" />   <interceptor-ref name="myInterceptor2" >    使用拦截器时对hello赋值,会改变引用时的赋值    <param name="hello">welcome</param>   </interceptor-ref>   <interceptor-ref name="defaultStack" />  手动导入Struts2默认的拦截器栈  </action>  定义默认的拦截器栈  他会自动的把默认的拦截器附加到每一个Action中去  在一个包中,可以有0或1个默认的拦截器栈  如果在一个<action>中手动的指定一个拦截器,默认的拦截器就不会再自动的加到这个action里  只能通过手工的方式再导入一下  <default-interceptor-ref name="defaultStack" /> </package></struts>  

第三中方式:指定拦截的方法
 
如果调用多个拦截器,执行顺序是怎样的?
如:
<interceptors> <interceptor name="myInterceptor" class="..."></interceptor> <interceptor name="myInterceptor2" class="..."></interceptor></interceptors><interceptors-stack name="myInterceptorStack"> <!-- 执行顺序 按配置的顺序 --> <interceptor-ref name="myInterceptor" /> <interceptor-ref name="myInterceptor2" /></interceptors-stack>

注:拦截器会在调用invoke()方法调用之前和之后都会执行
   拦截器1                          1 中invoke方法前的语句
     |_________拦截器2              2 中invoke方法后的语句
                     |__________
                                    |
                                 invoke()   执行invoke()方法
                      __________|
                     |
      ________拦截器2               2 中invoke方法后的语句
     |
   拦截器1                          1 中invoke方法后的语句
 
==================================================
如果拦截器没有做任何配合话,它会拦截所有的逻辑执行方法
如果<action>中method="execute" 指定了方法,它便只拦截execute方法
还可以通过方法过滤拦截器来指定拦截或不拦截的方法

--------------------------------------------------
继承MethodFilterInterceptor
public class MyInterceptor3 extends MethodFilterInterceptor { @Override public void init() {  System.out.println("init3"); } //intercept()已经实现好了,不用去管他 //需要重写一下doIntercept @Override public String doIntercept(ActionInvocation invocation) throws Exception {  System.out.println("intercept()3...");  String result = invocation.invoke();  System.out.println("finish3...");  return result; }}


---------------------------------------------------
MethodFilterInterceptor中包含两个protected的属性
 Set includeMethods : 包含谁
 Set excludeMethods : 排除谁

<interceptors>
 <interceptor name="myInterceptor3" class="..."></interceptor>
</interceptors>

<action ...>
 <result ...>...</result>
 ...
 
 <interceptor-ref name="myInterceptor3" >
  包含                         要拦截方法 多个用逗号分开
  <param name="includeMethods">execute,test</param>
  
  排除                         不拦截的方法 多个用逗号分开
  <param name="excludeMethods">execute</param>
 </interceptor-ref>
 <interceptor-ref name="defaulteStack" />
</action>

六、自定义拦截器的使用示例

1、定义拦截器

public class LoginInterceptor extends MethodFilterInterceptor{@Overrideprotected String doIntercept(ActionInvocation arg0) throws Exception {Map<String,Object> session = ActionContext.getContext().getSession();User user = (User)session.get("user");if(user==null){return "index";}return arg0.invoke();}

2、struts.xml的配置

<package name="default" namespace="" extends="struts-default">        <!-- 定义拦截器 -->        <interceptors>            <!-- 定义拦截器 -->            <interceptor name="login" class="loginInterceptor">                                   <!-- 定义拦截器栈 -->            <interceptor-stack name="loginStack">                                <!-- 拦截器的使用 -->                <interceptor-ref name="login">                                        <!-- 指定拦截器的方法:excludeMethods:不包括的方法;includeMethods:包含的方法 -->                    <param name="excludeMethods">login,register,execute</param>                                     </interceptor-ref>                                <!-- 下面这句一定要加上 -->                <interceptor-ref name="defaultStack"></interceptor-ref>                            </interceptor-stack>        </interceptors>                <!-- 定义全局拦截器 -->        <default-interceptor-ref name="loginStack"/>                <!-- 定义全局处理结果 -->        <global-results>            <!-- 逻辑名为index的结果,映射到/login.jsp页面 -->             <result name="index" type="redirect">/login.jsp</result>       </global-results>               <action name="userLogin" class="userAction" method="login">            <result name="success">/jsp/index.jsp</result>            <result name="error">/login.jsp</result>        </action>                <action name="userRegister" class="userAction" method="register">            <result name="fail">/jsp/user/register.jsp</result>            <result name="success">/login.jsp</result>        </action>                  <action name="rand" class="randomAction">                <result type="stream">                     <param name="contentType">image/jpeg</param>                     <param name="inputName">inputStream</param>                </result>            </action>     </package></struts>



原创粉丝点击