Struts2 学习笔记18 拦截器原理分析
我们来进行一下拦截器的原理分析,从Struts2的源代码开始,然后我们手动创建一个项目进行模拟。(源代码需要下载然后添加好才能看到)我们可以用Debug来读源码。
从doFilter开始执行,流程如图。
下面是Struts2官方的图。
这其中主要要解释的就是重复调用一个个interceptor(是从配置文件中读取的),当全部调用完成后,再调用Action。
下面我们用一个小项目来模拟一下,就很好理解了(项目中的类名和Struts2中的相同或类似)。
我们新建了一个Java Project。
代码如下。
public class Action {
public void execute(){
System.out.println("execute!");
}
}
import java.util.ArrayList;
import java.util.List; public class ActionInvocation {
List<Interceptor> interceptors = new ArrayList<Interceptor>();
int index = -1;
Action a = new Action(); public ActionInvocation(){
this.interceptors.add(new FirstInterceptor());
this.interceptors.add(new SecondInterceptor());
} public void invoke(){
index++;
if(index >= interceptors.size()){
a.execute();
}
else { this.interceptors.get(index).intercept(this);
}
}
}
public class FirstInterceptor implements Interceptor{ @Override
public void intercept(ActionInvocation incocation) {
System.out.println(1);
incocation.invoke();
System.out.println(-1); } }
public class SecondInterceptor implements Interceptor{ @Override
public void intercept(ActionInvocation incocation) {
System.out.println(2);
incocation.invoke();
System.out.println(-2); } }
public interface Interceptor {
public void intercept(ActionInvocation incocation); }
public class Main {
public static void main(String args[]){
new ActionInvocation().invoke();
}
}
结果如图。
我们可以从结果中看到,进入FirstInterceptor中被拦截,然后调用SecondInterceptor,然后调用Action,再返回SecondInterceptor中,再进入FirstInterceptor,这是方法的执行过程,在Struts2中是调用配置文件中的interceptor,而在我们这个项目中是手动add的。
过程可以看图。
Struts2 学习笔记18 拦截器原理分析的更多相关文章
- Struts2学习笔记(拦截器配置添加)
一.拦截器工作原理: 根据Struts2的工作原理图,拦截器在action执行前进行顺序调用,之后执行Action并返回结果字符串,再逆序调用拦截器.(结构类似递归方式...)大部分时候,拦截器方法都 ...
- Struts2学习笔记五 拦截器
拦截器,在AOP中用于在某个方法或字段被访问之前,进行拦截,然后在之前或之后加入某些操作.拦截是AOP的一种实现策略. Struts2中,拦截器是动态拦截Action调用的对象.它提供了一种机制可以使 ...
- spring mvc拦截器原理分析
我的springMVC+mybatis中的interceptor使用@autowired注入DAO失败,导致报空指针错误,这个是为什么呢? :空指针说明没有注入进来,你可以检查一下你的这个拦截器int ...
- struts2学习(6)自定义拦截器-登录验证拦截器
需求:对登录进行验证,用户名cy 密码123456才能登录进去: 登录进去后,将用户存在session中: 其他链接要来访问(除了登录链接),首先验证是否登录,对这个进行拦截: com.cy.mod ...
- SpringMVC学习笔记:拦截器和过滤器
首先说明一下二者的区别: 1. 拦截器基于java的反射机制,而过滤器是基于函数回调 2. 拦截器不依赖于servlet容器,过滤器依赖servlet容器 3. 拦截器只能对action请求起作用,而 ...
- SpringMVC学习笔记九:拦截器及拦截器的简单实用
SpringMVC中的interceptor拦截器是非常重要的,它的主要作用就是拦截指定的用户请求,并进行相应的预处理和后处理. 拦截时间点在"处理器映射器根据用户提交的请求映射出所要执行的 ...
- SpringMVC 学习笔记(六)拦截器
5.1.处理器拦截器简介 Spring Web MVC的处理器拦截器(如无特殊说明,下文所说的拦截器即处理器拦截器) 类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理. ...
- Python学习笔记之装饰器原理
def decorator(fn): def wrapper(): print("询价") fn() print("购买成功!") return wrapper ...
- Spring学习笔记——Spring依赖注入原理分析
我们知道Spring的依赖注入有四种方式,各自是get/set方法注入.构造器注入.静态工厂方法注入.实例工厂方法注入 以下我们先分析下这几种注入方式 1.get/set方法注入 public cla ...
随机推荐
- Codeforces 85D Sum of Medians(线段树)
题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...
- winform 之1---窗体介绍
窗体是winform开发的基础,需要掌握窗体的创建.属性.调用和窗体传值等等. 1.在项目中添加窗体很简单,在项目上点击右键,选择添加windows窗体即可为项目添加新的窗体. 2.窗体的属性设置,右 ...
- Arduino 入门程序示例之一个 LED(2015-06-11)
前言 答应了群主写一些示例程序,一直拖延拖延拖延唉.主要还是害怕在各大高手面前班门弄斧……(这也算是给拖延症找一个美好的理由吧),这几天终于下决心要写出来了,各位高手拍砖敬请轻拍啊. 示例程序 首先是 ...
- PostgreSQL正则表达式查询
参考http://www.php100.com/manual/PostgreSQL8/functions-matching.html LIKE string LIKE pattern [ ESCAPE ...
- 高级UIKit-07(AVAudioPlayer)
[day09-1-AVAudioPlayer]:播放音乐案例 实现多媒体需要准备以下两点: 需要引入一个框架AVFoundation.framework 然后引入#import <AVFound ...
- QtSoap调用Web Service(QtSoap是非官方应用)
今天学习如何用QtSoap访问Web Service服务.这里调用的是查询QQ在线状态的服务qqOnlineWebService.调用的几个步骤: 1.创建QtSoapMessage对象 messag ...
- java设计模式之——适配器模式
适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作. 适配器模式的用途 用电器做例子,笔记本电脑的插头一般都是三相的,即除了阳极.阴极 ...
- 存几个html画图的网站
http://jvectormap.com/ http://julying.com/lab/raphael-js/docs/#Paper.path http://www.highcharts.com/ ...
- HDU-1664-Different Digits(BFS)
Problem Description Given a positive integer n, your task is to find a positive integer m, which is ...
- 当JAVA集合移除自身集合元素时发生的诸多问题
一段代码目的是想删除集合中包括"a"字符串的集合项: public class TestForeach { public static void main(String[] arg ...