struts2拦截器

   
实现拦截器类:


   如果用户要开发自己的拦截器类,应该实现 com.opensymphony.xwork2.inteceptor.Interceptor接口,
该接口代码如下:


   public interface Interceptor extends Serializable
   {


    //销毁该拦截器钱的回调方法
    void destory();


    //初始化该拦截器的回调方法
    void init();


    //拦截器实现拦截的逻辑方法
    String intercept(ActionInvocation invocation) throws Exception;
       
       invocation.invoke(); //执行下一个拦截器或则进入Action的方法;
   }


方法解释:
   
 init():在该拦截器初始化之后,在该拦截器执行拦截之前,系统将回调该方法。只执行一次。该方法主要用于打开一些一次性资源,例如数据库资源等;


 destory(): 该方法与init()方法对应;在拦截器实例被销毁之前,系统将回调该烂机器的destory方法,该方法主要用于销毁在init()方法里打开的资源;


 intercept(ActionInvocation invocation): 该方法是用户需要实现的拦截动作。就像Action的execute方法一样,intercept 方法会返回一个字符串作为逻辑视图。如果该方法返回一个字符串,系统将会跳转到该逻辑视图对应的实际视图资源,不会调用被拦截的Action。该方法的ActionInvocation参数包含了被拦截的Action的引用,可以通过调用该参数的invoke方法,将控制权转给下一个拦截器,或者转给Action的execute方法;


--------------------------------------------------------------------------------------------------
注意: 


  除此之外,Struts2还提供了一个AbstractInterceptor类,该类提供了一个init和destory方法的空实现,如果我们实现的拦截器不需要申请资源,则可以无需实现这两个方法,可见继承AbstractInterceptor类实现自定义拦截器会更加简单。


例如:


 public class SimpleInterceptor extends AbstractInterceptor
 {
   
   //拦截Action方法的intercept方法
   public String intercept(ActionInvocation invocation) throws Exception
   {
      //取得被拦截Action的实例
      LoginAction action = (LoginAction ) invacation.getAction();
   
      System.out.println("在使用Action操作之前的方法插入处");
  
      //执行该拦截器的后一个的拦截器,或者直接指定Action的指定方法
      String result = invacation.invoke();


      System.out.println("在使用Action操作之后的方法插入处");    
  
      return result;
    }


}
--------------------------------------------------------------------------------------------------


深入拦截器:


    方法过滤:默认情况下我们自定义的拦截器,会拦截Action所有的方法,若我们不希望拦截这个Action所有的方法,那么我们就需要使用Struts2的方法过滤特性;


    为实现方法过滤特性,struts2提供了一个MethodFilterInterceptor类,该类是AbstractInterceptor类的子类;用户需要自己的拦截器支持方法过滤特性,则应该继承MethodFilterInterceptor;
    
    MethodFilterInterceptor类提供了一个doIntercept(ActionInvocation invocation)抽象方法。实现对Action拦截;


     MethodFilterInterceptor方法,额外增加了如下两个方法:


 public void setExcludeMethods(String excludeMethods):排除需要过滤的方法---设置方法“黑名单”,所有在excludeMethods字符串中列出的方法都不会被拦截;
 
 public void setIncludeMethods(String includeMethods):排除需要过滤的方法---设置方法“白名单”,所有在includeMethods字符串中列出的方法都会被拦截;


--------------------------------------------------------------------------------------------------
  使用方法过滤器:


   <interceptors>
      <!-- 配置方法过滤的拦截器 -->
      <interceptor name="myfilter" class="lee.MyFilterInterceptor">
   </interceptors>
 
   <action name="login" class="lee.LoginAction">
    <interceptor-ref name="myfilter">
      <!-- 指定execute和add方法不被拦截 -->
      <param name="excludeMethods">execute,add</param>
    </interceptor-ref>
   </action>




--------------------------------------------------------------------------------------------------


  拦截器执行顺序:


    结论:在被拦截方法之前的拦截动作,配置在前面的拦截器将会先对用户请求起作用;如果是在被拦截方法之后的拦截动作,配置在前面的拦截器则会后对用户请求起作用;


-------------------------------------------------------------------------------------------------
  定义拦截器


  定义拦截器栈之前,必须先定义组成拦截器栈的多个拦截器。struts2把拦截器栈当成拦截器处理。
因此拦截器和拦截器栈都放在<interceptors.../>元素中定义。
  例如:
 
  <interceptors>
     <!-- 定义权限检查的拦截器 -->
     <interceptor name="authority"  class="cn.edu.zust.AuthorityInterceptor" />
     <!-- 定义日志记录的拦截器 -->
     <interceptor name="log"  class="cn.edu.zust.LogInterceptor" />    
     <!-- 定义拦截器栈 -->
     <interceptor-stack name="authorityandlog" >    
          <!-- 定义该拦截器栈里包含authority拦截器 -->
          <interceptor-ref  name="authority" />
          <!-- 定义该拦截器栈里包含log拦截器 -->
          <interceptor-ref  name="log" />
     </interceptor-stack>
  </interceptors>


  使用拦截器:
  
   在上面我们定义了两个拦截器,并将两个拦截器组成了一个拦截器栈。一旦定义了拦截器和拦截器栈
之后,在Action中使用拦截器和拦截器栈的方法是相同的。
  
   例如:
   <action name="myAction" class="cn.edu.zust.myAction">
     <result name="success">login.jsp</result>
     <!-- 调用自定义拦截器栈 -->
     <interceptor-ref  name="authorityandlog" />
   </action>


注意:对于Action而言,引用拦截器和拦截器栈的用法是完全一样的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值