public class ActionProxy {

    public static void main(String[] args) {

        //模拟ActionProxy调用invoke()方法
ActionInvocation invocation=new ActionInvocation();
invocation.invoke();
}
}
public class ActionInvocation {

    //成员变量  拦截器数组,访问的action 对象
List<Interceptor> list=new ArrayList<>();
Action action=new Action();
private int index=-1; //初始化list对象,装inceptor对象
public ActionInvocation() {
FirstInterceptor firstInterceptor=new FirstInterceptor();
SecondInterceptor secondInterceptor=new SecondInterceptor();
this.list.add(firstInterceptor);
this.list.add(secondInterceptor);
} public void invoke(){
index++;
if (index>list.size()-1) {
//拦截器已经执行完,执行action
action.execute();
}else {
//调用拦截器的拦截方法
list.get(index).intercept(this);
}
} }
public interface Interceptor {

    public void intercept(ActionInvocation invocation);
}
public class FirstInterceptor implements Interceptor{

    @Override
public void intercept(ActionInvocation invocation) {
// TODO Auto-generated method stub
//前处理
System.out.println("第一个拦截器开始");
//调用invocation 的invoke方法
invocation.invoke();
//后处理
System.out.println("第一个拦截器结束");
} }
public class SecondInterceptor implements Interceptor{

    @Override
public void intercept(ActionInvocation invocation) {
// TODO Auto-generated method stub
//前处理
System.out.println("第二个拦截器开始");
//调用invocation 的invoke方法
invocation.invoke();
//后处理
System.out.println("第二个拦截器结束");
} }
public class Action {

    public void execute(){
System.out.println("action 执行了!!!");
}
}
result:
第一个拦截器开始
第二个拦截器开始
action 执行了!!!
第二个拦截器结束
第一个拦截器结束

struts2拦截器执行模拟 参考马士兵老师的更多相关文章

  1. struts2拦截器的实现机制

    前言 最近老大让每周写一篇技术性的博客,想想也没啥写,就想着随便拿个以前的项目去研究研究五大框架的底层代码.本人水平有限,有不对的地方还望大家勿喷,指正! 开始之前先了解下strtus2的工作流程: ...

  2. Struts2拦截器的执行过程浅析

    在学习Struts2的过程中对拦截器和动作类的执行过程一度陷入误区,特别读了一下Struts2的源码,将自己的收获分享给正在困惑的童鞋... 开始先上图: 从Struts2的图可以看出当浏览器发出请求 ...

  3. Struts2拦截器模拟

    前言: 接触Struts2已经有一段时间,Student核心内容就是通过拦截器对接Action,实现View层的控制跳转.本文根据自身理解对Struts2进行一个Java实例的模拟,方便大家理解! 示 ...

  4. Struts2拦截器总结

    拦截器的本质: 拦截器就是一个类,一个实现了超级接口Interceptor的类.Interceptor接口里定义了三个方法 init(),destory(),intercept().其中inercep ...

  5. struts2(五)之struts2拦截器与自定义拦截器

    前言 前面介绍了struts2的输入验证,如果让我自己选的话,肯定是选择xml配置校验的方法,因为,能使用struts2中的一些校验规则,就无需自己编写了, 不过到后面应该都有其他更方便的校验方法,而 ...

  6. Struts2拦截器说明

    有关于Struts2的拦截器的原理 在此共设置了两个拦截器,firstInterception.SecondInterception package struts2_inteception; publ ...

  7. Struts2拦截器原理

    拦截器是struts2处理的核心,本文主要说struts2的拦截器的基本原理/实现,其它框架处理的东西就不说了,得自己再看了.struts2版本:2.2.3当一个请求来了后,从org.apache.s ...

  8. 7.Struts2拦截器及源码分析

    1.Struts2架构图 2.Struts2 执行过程分析 1.首先,因为使用 struts2 框架,请求被Struts2Filter 拦截 2.Struts2Filter  调用 DisPatche ...

  9. struts2动态调用+Servlet过滤器+struts2拦截器

    周末真的是懒到心慌...... 本文是在完整s2sh项目基础上添加的,不太了解s2sh项目构建的朋友可以先参考一下这几篇文章: eclipse环境下基于tomcat-7.0.82构建struts2项目 ...

随机推荐

  1. CSS技巧-文字分散对齐的方法

    下面的代码可以在IE中实现文字分散对齐: <table width="300" align="center">    <tr>      ...

  2. new_images

    /home/westward/Pictures/Screenshot from 2017-03-19 20:23:55.png

  3. 使用DDMS查看设备内的文件系统

    system文件系统存储了一些系统相关的文件 system/app里面是系统自带的应用程序 system/fonts里面存放的是系统自带的字体 system/frameworks里面存放的是系统的一些 ...

  4. Linux Centos关机命令

    centos关机命令: 1.halt 立马关机 2.shutdown -h 10 1分钟后自动关机 3.poweroff 立刻关机,并且电源也会断掉 4.shutdown -h now 立刻关机(ro ...

  5. SpringWeb项目常用注解简单介绍

    注解分为两类: 一类是使用Bean,即是把已经在xml文件中配置好的Bean拿来用,完成属性.方法的组装:比如@Autowired , @Resource,可以通过byTYPE(@Autowired) ...

  6. TensorFlow学习笔记——节点(constant、placeholder、Variable)

    一. constant(常量) constant是TensorFlow的常量节点,通过constant方法创建,其是计算图(Computational Graph)中的起始节点,是传入数据. 创建方式 ...

  7. 踩坑 net core

    webclient   可以替换为 HttpClient 下载获取url的内容: 证书: https://stackoverflow.com/questions/40014047/add-client ...

  8. Spring boot 导出Excel

    Html页面: window.location.href="adjectfkController/exportTemplate?adjOrg="+ adjOrg +"&a ...

  9. BCM5396的SPI理解

    参考文档链接:https://pan.baidu.com/s/1kuXJmULwtjOW1TeOuTRPQQ *时钟极性和相位 BCM538X / BCM5396用于根据以下标准发送/接收SPI数据: ...

  10. .NET界面控件DevExpress全新发布v18.2.6|附下载

    DevExpress Universal Subscription(又名DevExpress宇宙版或DXperience Universal Suite)是全球使用广泛的.NET用户界面控件套包,De ...