Struts2-拦截器配置

<interceptor>定义一个拦截器

  • name属性定义拦截器的引用名称
  • class属性定义拦截器类

<interceptor-stack>定义一组拦截器的组合

  • 内部使用interceptor-ref标签引用已经定义了的拦截器
  • name属性定义拦截器组的引用名称

<interceptor-ref>定义对拦截器的引用

  • name属性指向已经定义了的拦截器名

<default-interceptor-ref> 指明默认的拦截器,如果action没有自定义拦截器的引用,则应用默认的

<struts>    
    <!-- 
        注意:一旦继承了struts-default包(package),所有Action都会默认调用拦截器栈 defaultStack。
        但是当在Action配置中加入“<interceptor-ref name=”..“ />”则会覆盖defaultStack,
        所以在action中写拦截器引用时,需要显示引用defaultStack(而且最好在第一句)
        <interceptor-ref name="defaultStack"></interceptor-ref>
        <interceptor-ref name="myint1"></interceptor-ref>
     -->



    <!-- 自定义拦截器
        1,创建拦截器类
        2,定义拦截器
        3,配置action使用拦截器
     -->
    <package name="myinterceptor" extends="struts-default">
        <!-- 拦截器配置
            1, 配置<interceptors> 里面时拦截器的定义及拦截器栈
         -->    
         <interceptors>
             <!-- 拦截器 是对拦截器的定义
             <interceptor name="拦截器名" class="对应的类"></interceptor>
             -->
             <interceptor name="myint1" class="com.java4struts.intercepter.MyInterceptor"></interceptor>
             <interceptor name="myint2" class="com.java4struts.intercepter.MyInterceptor2"></interceptor>
             <!-- 方法过滤拦截器
                 只要(有)在includemethods参数中出现的方法名都拦截
                 在includeMethods中没出现,而且在excludeMethods中出现的,不拦截
              -->
             <interceptor name="mymethodint" 
                 class="com.java4struts.intercepter.MyMethodFilterInterceptor">
                     <param name="excludeMethods">abc</param>
                     <param name="includeMethods">xyz,execute,abc</param>
             </interceptor>
         
              
              
             <!-- 拦截器栈:一组拦截器 -->
             <interceptor-stack name="mystack">
                 <!-- 拦截器的引用 
                 <interceptor-ref name="拦截器名"></interceptor-ref>
                 -->
                 <interceptor-ref name="defaultStack"></interceptor-ref>
                 <interceptor-ref name="myint1"></interceptor-ref>
                 <interceptor-ref name="myint2"></interceptor-ref>
                 <interceptor-ref name="mymethodint"></interceptor-ref>
                 
             </interceptor-stack>
             
         </interceptors>
         
         
         <action name="demoaction" class="com.java4struts.intercepter.DemoAction" >
             <!-- 在action中应用拦截器 
             注意:一旦应用了自定义的拦截器配置,那么默认拦截器将不再应用,需要明确指明,
             这样才能实现action的默认功能
             <interceptor-ref name="defaultStack"></interceptor-ref>
             <interceptor-ref name="拦截器名"></interceptor-ref>
             -->
             <!-- 第一种方法:单独引用每一个拦截器-->
                 <interceptor-ref name="defaultStack"></interceptor-ref>
                 <interceptor-ref name="myint1"></interceptor-ref>
                 <interceptor-ref name="myint2"></interceptor-ref>
                 <interceptor-ref name="mymethodint"></interceptor-ref>
              
             <!-- 第二种方法:定义拦截器栈 
             <interceptor-ref name="mystack"></interceptor-ref>
             -->
             
             <result name="success">/WEB-INF/securitypages/security.jsp</result>
         </action>    
         
    </package>
</struts>

拦截器的三种实现方式

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class MyMethodFilterInterceptor extends MethodFilterInterceptor {

    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {
        //这个方法实现拦截器应用
        //1,调用action方法前的处理操作
        System.out.println("MyMethodFilterInterceptor action方法调用前");
        
        //2,调用下一个拦截器或action方法(如果没有下一个拦截器)
        String result = invocation.invoke();        
        
        //3,action(或下一个拦截器)返回结果后的处理操作
        System.out.println("MyMethodFilterInterceptor action方法调用后");
        return result;
    }


}
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class MyInterceptor2 extends AbstractInterceptor {

    public String intercept(ActionInvocation invocation) throws Exception {
        //这个方法实现拦截器应用
        //1,调用action方法前的处理操作
        System.out.println("MyInterceptor2 action方法调用前");
        
        //2,调用下一个拦截器或action方法(如果没有下一个拦截器)
        String result = invocation.invoke();        
        
        //3,action(或下一个拦截器)返回结果后的处理操作
        System.out.println("MyInterceptor2 action方法调用后");
        return result;
    }

}
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class MyInterceptor implements Interceptor {

    public void destroy() {
        // 销毁拦截器时被调用
        System.out.println("MyInterceptor被销毁了");
    }

    public void init() {
        // 实例化拦截器之后被调用
        System.out.println("MyInterceptor初始化"); 
    }

    public String intercept(ActionInvocation invocation) throws Exception {
        //这个方法实现拦截器应用
        //1,调用action方法前的处理操作
        System.out.println("MyInterceptor1 action方法调用前");
        
        //2,调用下一个拦截器或action方法(如果没有下一个拦截器)
        String result = invocation.invoke();        
        
        //3,action(或下一个拦截器)返回结果后的处理操作
        System.out.println("MyInterceptor1 action方法调用后");
        return result;
    }

}

 

转载于:https://www.cnblogs.com/zhangmiao/archive/2013/03/16/2962607.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值