1. public class ActionProxy {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. //模拟ActionProxy调用invoke()方法
  6. ActionInvocation invocation=new ActionInvocation();
  7. invocation.invoke();
  8. }
  9. }
  1. public class ActionInvocation {
  2.  
  3. //成员变量 拦截器数组,访问的action 对象
  4. List<Interceptor> list=new ArrayList<>();
  5. Action action=new Action();
  6. private int index=-1;
  7.  
  8. //初始化list对象,装inceptor对象
  9. public ActionInvocation() {
  10. FirstInterceptor firstInterceptor=new FirstInterceptor();
  11. SecondInterceptor secondInterceptor=new SecondInterceptor();
  12. this.list.add(firstInterceptor);
  13. this.list.add(secondInterceptor);
  14. }
  15.  
  16. public void invoke(){
  17. index++;
  18. if (index>list.size()-1) {
  19. //拦截器已经执行完,执行action
  20. action.execute();
  21. }else {
  22. //调用拦截器的拦截方法
  23. list.get(index).intercept(this);
  24. }
  25. }
  26.  
  27. }
  1. public interface Interceptor {
  2.  
  3. public void intercept(ActionInvocation invocation);
  4. }
  1. public class FirstInterceptor implements Interceptor{
  2.  
  3. @Override
  4. public void intercept(ActionInvocation invocation) {
  5. // TODO Auto-generated method stub
  6. //前处理
  7. System.out.println("第一个拦截器开始");
  8. //调用invocation 的invoke方法
  9. invocation.invoke();
  10. //后处理
  11. System.out.println("第一个拦截器结束");
  12. }
  13.  
  14. }
  1. public class SecondInterceptor implements Interceptor{
  2.  
  3. @Override
  4. public void intercept(ActionInvocation invocation) {
  5. // TODO Auto-generated method stub
  6. //前处理
  7. System.out.println("第二个拦截器开始");
  8. //调用invocation 的invoke方法
  9. invocation.invoke();
  10. //后处理
  11. System.out.println("第二个拦截器结束");
  12. }
  13.  
  14. }
  1. public class Action {
  2.  
  3. public void execute(){
  4. System.out.println("action 执行了!!!");
  5. }
  6. }
  1. result
  2. 第一个拦截器开始
  3. 第二个拦截器开始
  4. action 执行了!!!
  5. 第二个拦截器结束
  6. 第一个拦截器结束

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. 延时显示(类QQ头像显示)

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...

  2. log4j不输出日志的解决方案

    参考:http://blog.csdn.net/qq994406030/article/details/53433159 主要是log4j.properties log权限和log输出方式没配好.

  3. [POJ3481]Double Queue

    Problem 0 结束操作 1 K P 将一个数K以优先级P加入 2 取出优先级最高的那个数 3 取出优先级最低的那个数 Solution Splay模板题 Notice 是输出数而不是输出优先级. ...

  4. flask-admin fileadmin 上传文件,中文名的解决方案 重写部分secure_filename

    class upload_view(FileAdmin): def _save_form_files(self, directory, path, form): super() filename = ...

  5. 一: Docker的概念

    附件:https://files.cnblogs.com/files/chaos-li/docker-k8s-devops-master-9287a2ca56433ca076078b564de9488 ...

  6. 十. Python基础(10)--装饰器

    十. Python基础(10)--装饰器 1 ● 装饰器 A decorator is a function that take a function as an argument and retur ...

  7. 二. Python基础(2)--语法

    二. Python基础(2)--语法 1.实现一个简单的登录系统 '''# 形式1 n = 1 while n < 4:     name = input("请输入姓名\n" ...

  8. 深入理解java虚拟机---Class文件(二十)

    无符号数.表 当实现了不同语言的编译器,比如jython,jruby等等,那么就可以利用这些语言编写代码,通过各自的编译器编译成符合jvm规范的字节码文件,就可以利用jvm来执行了. Class文件在 ...

  9. 深入理解java虚拟机---虚拟机工具jps与jstat(十四)

    jps-java process status jps类似于linux的ps命令,用于查看进程. JPS 名称: jps - Java Virtual Machine Process Status T ...

  10. Codeforces Round #495 (Div. 2) B

    题目链接:http://codeforces.com/contest/1004/problem/B B. Sonya and Exhibition time limit per test 1 seco ...