struts2拦截器执行模拟 参考马士兵老师
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拦截器执行模拟 参考马士兵老师的更多相关文章
- struts2拦截器的实现机制
前言 最近老大让每周写一篇技术性的博客,想想也没啥写,就想着随便拿个以前的项目去研究研究五大框架的底层代码.本人水平有限,有不对的地方还望大家勿喷,指正! 开始之前先了解下strtus2的工作流程: ...
- Struts2拦截器的执行过程浅析
在学习Struts2的过程中对拦截器和动作类的执行过程一度陷入误区,特别读了一下Struts2的源码,将自己的收获分享给正在困惑的童鞋... 开始先上图: 从Struts2的图可以看出当浏览器发出请求 ...
- Struts2拦截器模拟
前言: 接触Struts2已经有一段时间,Student核心内容就是通过拦截器对接Action,实现View层的控制跳转.本文根据自身理解对Struts2进行一个Java实例的模拟,方便大家理解! 示 ...
- Struts2拦截器总结
拦截器的本质: 拦截器就是一个类,一个实现了超级接口Interceptor的类.Interceptor接口里定义了三个方法 init(),destory(),intercept().其中inercep ...
- struts2(五)之struts2拦截器与自定义拦截器
前言 前面介绍了struts2的输入验证,如果让我自己选的话,肯定是选择xml配置校验的方法,因为,能使用struts2中的一些校验规则,就无需自己编写了, 不过到后面应该都有其他更方便的校验方法,而 ...
- Struts2拦截器说明
有关于Struts2的拦截器的原理 在此共设置了两个拦截器,firstInterception.SecondInterception package struts2_inteception; publ ...
- Struts2拦截器原理
拦截器是struts2处理的核心,本文主要说struts2的拦截器的基本原理/实现,其它框架处理的东西就不说了,得自己再看了.struts2版本:2.2.3当一个请求来了后,从org.apache.s ...
- 7.Struts2拦截器及源码分析
1.Struts2架构图 2.Struts2 执行过程分析 1.首先,因为使用 struts2 框架,请求被Struts2Filter 拦截 2.Struts2Filter 调用 DisPatche ...
- struts2动态调用+Servlet过滤器+struts2拦截器
周末真的是懒到心慌...... 本文是在完整s2sh项目基础上添加的,不太了解s2sh项目构建的朋友可以先参考一下这几篇文章: eclipse环境下基于tomcat-7.0.82构建struts2项目 ...
随机推荐
- 1-find
查找算法 #include <stdio.h> #include <assert.h> #define FALSE 0 #if 1 //array method int fin ...
- Vue + Element UI 实现权限管理系统 (管理应用状态)
使用 Vuex 管理应用状态 1. 引入背景 像先前我们是有导航菜单栏收缩和展开功能的,但是因为组件封装的原因,隐藏按钮在头部组件,而导航菜单在导航菜单组件,这样就涉及到了组件收缩状态的共享问题.收缩 ...
- day04控制流程之if判断
一.控制流程之if判断 1.什么是if判断 判断一个条件如果成立则...不成立则... 2.为何要有if判断 让计算机能够像人一样具有判断能力 3.如何用if判断 ''' # 语法1: ''' if ...
- day17-json格式转换
Json简介:Json,全名 JavaScript Object Notation,是一种轻量级的数据交换格式.Json最广泛的应用是作为AJAX中web服务器和客户端的通讯的数据格式.现在也常用于h ...
- linux:SSH最简单教程
1.简介 ssh是一种用于计算机之间的加密登录协议.用户从本地计算机用ssh协议登录另一台计算机就可以认为登录安全,中途截获密码也不会泄露. 2.原理 (1)用户发登录请求给远程主机 (2)远程主机发 ...
- Java操作FTP工具类(实例详解)
这里使用Apache的FTP jar 包 没有使用Java自带的FTPjar包 工具类 package com.zit.ftp; import java.io.File; import java.i ...
- :复合模式:duck
#ifndef __QUAKEABLE_H__ #define __QUAKEABLE_H__ #include <iostream> #include <vector> us ...
- SQL-19 查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工
题目描述 查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工CREATE TABLE `departments` (`dept_no` c ...
- 3-D models provided some resources
http://d-earth.jamstec.go.jp/GAP_P4/ http://ds.iris.edu/ds/products/emc-earthmodels/
- 合并k个有序数组
给定K个有序数组,每个数组有n个元素,想把这些数组合并成一个有序数组 可以利用最小堆完成,时间复杂度是O(nklogk),具体过程如下: 创建一个大小为n*k的数组保存最后的结果创建一个大小为k的最小 ...