说说struts2中拦截器的请求流程一(模拟大致流程)

本文可作为北京尚学堂struts2课程的学习笔记。
首先 什么是拦截器?拦截器能干什么?
拦截器,顾名思义就是拦截对象然后做操作的东西,至于是拦截谁?那自然是拦截action了。能做什么操作呢?你想让action在运行之前干什么就能干什么,而且action本身并“不知道”自己被拦截了。
文章主要分析了拦截器部分的流程,对于环境的获取与初始化并没有涉及,对这部分感兴趣的朋友可以参考
http://www.cnblogs.com/liuling/p/2013-8-10-01.html
在真正进入源码分析阶段前,我先给大家一个小例子以帮助理解。

既然我们研究的是拦截器那么我们先写一个action和拦截器试试。

package inter;


public class FindNameAction {
	public void execute(){
		System.out.println("find name!");
	}
}

action没有继承任何接口,就只有一个execute。


package inter;

public interface Interceptor {
	void intercept(ActionInvocation invocation);
}

package inter;

public class FirstInterceptor implements Interceptor{

	@Override
	public void intercept(ActionInvocation invocation) {
		// TODO Auto-generated method stub
		System.out.println(1);
		invocation.invoke();
		System.out.println(-1);
	}
}

package inter;

public class SecondInterceptor implements Interceptor{

	@Override
	public void intercept(ActionInvocation invocation) {
		// TODO Auto-generated method stub
		System.out.println(2);
		invocation.invoke();
		System.out.println(-2);
	}
}
很简单吧 定义一个拦截器要实现的接口,然后有两个具体的拦截器。如果说代码里真有什么难懂的,就应该是ActionInvocaton了。别急,慢慢来。
ActionInvocation里面放置了,我们要最终操作的action类还有各种拦截器。
package inter;

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


public class ActionInvocation {
	List<Interceptor> interceptors=new ArrayList<Interceptor>();
	FindNameAction action=new FindNameAction();
	int index=-1;
	public ActionInvocation() {
		// TODO Auto-generated constructor stub
		interceptors.add(new FirstInterceptor());
		interceptors.add(new SecondInterceptor());
	}
	public void invoke(){
		index++;
		if (index<=interceptors.size()-1) 
			interceptors.get(index).intercept(this);
		else 
			action.execute();
	}
}
就像上面说的,ActionInvocation里面有两个变量。一个放置了所有的拦截器,一个是我们最终操作的action。
我们着重分析里面的invoke方法。
如果“拦截器栈”里面的拦截器还没有全部工作,就让它继续调用否则就直接做最核心的---最终的action工作。
纠结的地方就在这里了。上面我们定义的interceptor接口里,方法intercept的参数是一个ActionInvocation,在里面我们调用了ActionInvocation的invoke方法(在System.out.println(-1/-2)之前!!),而在invoke里面我们取出一个个interceptor又去调用他的intercept(参数是this,也就是ActionInvocation本身)。
循环往复了!!!
我们看看他的结果。
package inter;


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

测试结果如下:
1
2
find name!
-2
-1
如果大家对find name后面又出现了数字,而且还是-2 -1不是-1 -2还保持疑问的话。建议大家亲自画画图,理清楚调用的过程。

下一节 我们看看源码







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值