Struts2拦截器说明

有关于Struts2的拦截器的原理

在此共设置了两个拦截器,firstInterception、SecondInterception

 1 package struts2_inteception;
 2 
 3 public class firstInterception implements Interception{
 4     public void interceptor(ActionInvocaton invocation){
 5         System.out.println("-1");
 6         invocation.invoke();
 7         System.out.println("1");
 8         
 9     }
10 }
 1 package struts2_inteception;
 2 
 3 public class SecondInterception2 implements Interception{
 4     public void interceptor(ActionInvocaton invocation){
 5         System.out.println("-2");
 6         invocation.invoke();
 7         System.out.println("2");
 8         
 9     }
10 }

 

主函数Main类

1 package struts2_inteception;
2 
3 public class Main {
4     public static void main(String []args){
5         new ActionInvocaton().invoke();
6     }
7 
8 }

 

拦截器接口Interceptor

1 package struts2_inteception;
2 
3 public interface Interception {
4     public void interceptor(ActionInvocaton actionInvocaton);
5 }

 

一个模拟struts2的Action类

 

package struts2_inteception;

public class Action {
    public void execute(){
        System.out.println("execute()方法的执行");
    }

}

 

 

一个ActionInvocation类,

 

package struts2_inteception;

import java.util.List;
import java.util.ArrayList;


public class ActionInvocaton {
    int index=-1;
    Action action=new Action();
    List<Interception> interceptions=new ArrayList<Interception>();
    public ActionInvocaton(){
        //在此调用一系列的拦截器
        this.interceptions.add(new firstInterception());
        this.interceptions.add(new SecondInterception2());
    }
    public void invoke(){
        index++;
        if (index>=interceptions.size()){
            //调用action的方法
            action.execute();
        }else{
            //调用拦截器中加的东西
            this.interceptions.get(index).interceptor(this);
        }
        
    }
    
    

}

 真正的struts2的拦截器执行过程如下:

 

执行的结果如下:

-1
-2
execute()方法的执行
2
1

 

转载于:https://www.cnblogs.com/wbs19950305/p/8822994.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值