• Struts2中拦截器的使用与配置


    一,拦截器是什么?

    拦截器是在Action执行之前和之后执行的代码,是一个类似于过滤器的类;

    二,拦截器的作用

    拦截器拦截Action的请求,在Action之前或之后实现某项功能;

    三,拦截器的特点

    拦截器自由组合,有很高的灵活性,扩展性,有利于系统解耦;

    四,拦截器的应用场合

    1,struts2的大部分功能都是拦截器完成的,如:接收用户输入,数据验证,实现上传,国际化

    2,对action进行时间统计,权限控制等其它特定功能;

    3,对action添加功能,使用拦截器,action不需更改

    五,如何创建,配置拦截器

    1.一般拦截器

    2.方法拦截器

      1.    1.创建类继承AbstractInterceptor类,重写intercept方法

      2.   1: import com.opensymphony.xwork2.ActionInvocation;
        
          2: import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
        
          3: 
        
          4: public class TimerInterceptor extends AbstractInterceptor {
        
          5: 
        
          6:   @Override
        
          7:   public String intercept(ActionInvocation invocation) throws Exception {
        
          8:     
        
          9:     return invocation.invoke();
        
         10:   }
        
         11: }
          1.           

            配置拦截器:

    ------------1.定义过滤器:在package标签下添加<interceptors></interceptors>;在其下添加<interceptor>标签,指定name,class属性;

      1: <package name="xx" extends="struts-default" namespace="/">  
    
      2:     <interceptors>
    
      3:       <interceptor name="timer" class="com.interceptor.TimerInterceptor" />
    
      4:     </interceptors>
    
      5: </package>

    -----------2.指定引用的拦截器,拦截器栈:在action下添加<interceptor-ref name=”名”>,指定name属性

      1: <action name="helloworld" class="com.action.HelloAction">
    
      2: .....
    
      3: <interceptor-ref name="拦截器名/拦截器栈名" />
    
      4: </action>

     

    ------------3.定义拦截器器栈:

    在<interceptors></interceptors>标签下添加<interceptor-stack>标签,在其下添加指定应用的拦截器

      1: <interceptor-stack name="myStack">
    
      2:         <interceptor-ref name="defaultStack"/>
    
      3:         <interceptor-ref name="author"/>
    
      4:         <interceptor-ref name="timer"/>
    
      5: </interceptor-stack>

    2.方法拦截器

    继承MethodFilterInterceptor类,重写doIntercep()方法

      1: import com.opensymphony.xwork2.ActionInvocation;
    
      2: import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    
      3: 
    
      4: public class AuthorInterceptor extends AbstractInterceptor {
    
      5:   @Override
    
      6:   public String intercept(ActionInvocation invocation) throws Exception {
    
      7:       ....
    
      8:       return invocation.invoke();  
    
      9:   }
    
     10: }

    配置方法拦截器:

    <interceptors><interceptor name=”” class=”” /></interceptors>

    指定引用的拦截器,指定拦截的方法:

    <!--指定需要拦截的方法—>

    <interceptor-ref name=”includeMethods”>method1,method2…</interceptor-ref>

    <!—指定不拦截的方法—>

    <interceptor-ref name=”excludeMethods”>method3,..</interceptor-ref>

     

     

    例:1.使用拦截器控制用户访问权限;

     

           2.Struts2实现文件上传,使用拦截器限制文件大小,文件类型

     

     

    ===========================================================

    OVER

  • 相关阅读:
    Lookup Method
    ReLocated Record in Grid
    Call Form By FormString
    Send Mail
    十大已亡或正在灭亡的电脑技术
    一个程序员的内心自白摘录
    数据库中null和''具体的区别
    gridview 横向显示
    Exchange2007获取OWA邮箱容量的代码
    owa实现清空邮箱的代码
  • 原文地址:https://www.cnblogs.com/next-open/p/3461766.html
Copyright © 2020-2023  润新知