随笔 - 45, 文章 - 2, 评论 - 11, 引用 - 0
数据加载中……

拦截器的实现原理

运行主类
Main.java

public class Main {
    public static void main(String[] args) {
        new ActionInterceptor().invoke() ;
    }
}

/********************************/

拦截器核心
ActionInterceptor.java

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

public class ActionInterceptor {
    List<Inte> intes = new ArrayList<Inte>() ;
    int index = -1 ;
    public ActionInterceptor() {
        intes.add(new FistInte()) ;
        intes.add(new SecondInte()) ;
    }
    public void invoke() {
        index++ ;
        if(index >= intes.size()) {
            new Action().execute() ;
        }else {
            this.intes.get(index).inte(this) ;
        }
    }
}

/********************************/

接口:与拦截器之间的通讯接口
Inte.java

public interface Inte {
    void inte(ActionInterceptor actioninterceptor) ;
}

/********************************/

接口使用类
FistInte.java

public class FistInte implements Inte {

    @Override
    public void inte(ActionInterceptor actioninterceptor) {
        System.out.println(1) ;
        actioninterceptor.invoke() ;
        System.out.println(-1) ;
    }
}

SecondInte.java
public class SecondInte implements Inte {

    @Override
    public void inte(ActionInterceptor actioninterceptor) {
        System.out.println(2) ;
        actioninterceptor.invoke() ;
        System.out.println(-2) ;
    }
}

/********************************/

动作类

Action.java
public class Action {
    public void execute() {
        System.out.println("execute!") ;
    }
}

posted on 2010-07-31 15:19 jack zhai 阅读(437) 评论(0)  编辑  收藏 所属分类: Struts2


只有注册用户登录后才能发表评论。


网站导航: