Struts2拦截器原理
struts2版本:2.2.3
当一个请求来了后,从org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 开始处理
| public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req; try {
if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) { //包装request,对有文件上传的特殊处理下 request = prepare.wrapRequest(request); //查找对应的ActionMapping ActionMapping mapping = prepare.findActionMapping(request, response, true); //如果找不到ActionMapping则当作静态资源来处理 if (mapping == null) { //使用ActionMapping来执行action execute.executeAction(request, response, mapping); |
跟踪execute.executeAction(),则到了 org.apache.struts2.dispatcher.Dispatcher,如下:
| public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException { Map<String, Object> extraContext = createContextMap(request, response, mapping, context); // If there was a previous value stack, then create a new copy and pass it in to be used by the new Action String timerKey = "Handling request from Dispatcher"; Configuration config = configurationManager.getConfiguration();
ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy( request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack()); // if the ActionMapping says to go straight to a result, do it! //执行action proxy.execute(); // If there was a previous value stack then set it back onto the request |
DefaultActionProxyFactory创建ActionProxy,在com.opensymphony.xwork2.DefaultActionProxyFactory:
| public ActionProxy createActionProxy(String namespace, String actionName, String methodName, Map<String, Object> extraContext, boolean executeResult, boolean cleanupContext) { ActionInvocation inv = new DefaultActionInvocation(extraContext, true); container.inject(inv); return createActionProxy(inv, namespace, actionName, methodName, executeResult, cleanupContext); } |
接下来看看 org.apache.struts2.impl.StrutsActionProxy的execute()方法,如下:
| public String execute() throws Exception { ActionContext previous = ActionContext.getContext(); ActionContext.setContext(invocation.getInvocationContext()); try { //这里就是调用拦截器的入口了 return invocation.invoke(); } finally { if (cleanupContext) ActionContext.setContext(previous); } } |
最关键的,com.opensymphony.xwork2.DefaultActionInvocation.invoke()方法,这个DefaultActionInvocation是ActionInvocation的一个实现类,如下:
| //保存了执行当前action方法时需要调用的拦截器栈,按照struts.xml中配制的拦截器顺序,从前到后,依次加入到了这个Iterator里面 protected Iterator<InterceptorMapping> interceptors; public String invoke() throws Exception { if (executed) { //执行拦截器的intercept()方法,并将当前ActionInvocation对象传递给这个方法 resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this); // this is needed because the result will be executed, then control will return to the Interceptor, which will String _profileKey = "preResultListener: "; // now execute the result, if we're supposed to executed = true; return resultCode; |
基本原理到此为止,下面弄个小例子再说明一下:
| //拦截器,相当于struts2的拦截器 public interface Interceptor { String intercept(InvocationContext context); } //很多拦截器的实现 public class ExceptionInterceptor implements Interceptor { public String intercept(InvocationContext context) { public String intercept(InvocationContext context) { public String intercept(InvocationContext context) { //执行拦截器的invocation上下文,相当于struts2的ActionInvocation public class InvocationContext { // 这里存放当前执行当前action所需要执行的拦截器栈 public InvocationContext() { public String invoke() { private String executeAction() { //模拟请求进行测试 public class Test { public static void main(String[] args) { |
Struts2拦截器原理的更多相关文章
- Struts2拦截器原理以及实例
一.Struts2拦截器定义 1. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现. 2. ...
- struts2 javaweb 过滤器、监听器 拦截器 原理
转: 过滤器.监听器 拦截器 过滤器 创建一个 Filter 只需两个步骤: (1)创建 Filter 处理类: (2)在 web.xml 文件中配置 Filter . 创建 Filter 必须实现 ...
- 浅谈Struts2拦截器的原理与实现
拦截器与过滤器 拦截器是对调用的Action起作用,它提供了一种机制可以使开发者定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行.同时也是提供了 ...
- Struts2拦截器详解
一.Struts2拦截器原理: Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的 拦截器对象,然后串成一个列 ...
- struts2拦截器(四)
struts2拦截器原理: 当请求action时,struts2会查找配置文件,并根据配置实例化相对的 拦截器对象,然后串成一个列表,然后一个一个的调用列表中的拦截器. 比如:某些页面必须登录才可以访 ...
- Struts2拦截器的使用 (详解)
Struts2拦截器的使用 (详解) 如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈default ...
- Struts2拦截器模拟
前言: 接触Struts2已经有一段时间,Student核心内容就是通过拦截器对接Action,实现View层的控制跳转.本文根据自身理解对Struts2进行一个Java实例的模拟,方便大家理解! 示 ...
- struts2拦截器interceptor的三种配置方法
1.struts2拦截器interceptor的三种配置方法 方法1. 普通配置法 <struts> <package name="struts2" extend ...
- 从struts2拦截器到自定义拦截器
拦截器可谓struts2的核心了,最基本的bean的注入就是通过默认的拦截器实现的,一般在struts2.xml的配置中,package内直接或间接继承了struts-default.xml,这样st ...
随机推荐
- UVA 12034 Race(递推)
递推,f[i = i个名次][j = 共有j个人] = 方案数. 对于新加入的第j个人,如果并列之前的某个名次,那么i不变,有i个可供并列的名次选择,这部分是f[i][j-1]*i, 如果增加了一个名 ...
- 初学树套树:线段树套Treap
前言 树套树是一个十分神奇的算法,种类也有很多:像什么树状数组套主席树.树状数组套值域线段树.\(zkw\)线段树套\(vector\)等等. 不过,像我这么弱,当然只会最经典的 线段树套\(Trea ...
- tmux 用z关闭之后的恢复
ctrl+b 然后z是全屏 但是如果是ctrl+z就是关闭窗口了 tmux ls看所有窗口 然后 tmux attach -t 2或者3就恢复
- python_69_内置函数1
#abs()取绝对值 ''' all(iterable) Return True if all elements of the iterable are true (or if the iterabl ...
- 2.安装VS Code
1 打开网站 https://www.visualstudio.com/zh-hans/ 2. 安装 3.可以在程序目录命令行下 code . 用vscode 打开程序 4.下载插件 复制 ex ...
- 小波变换(wavelet transform)的通俗解释(一)
小波变换 小波,一个神奇的波,可长可短可胖可瘦(伸缩平移),当去学习小波的时候,第一个首先要做的就是回顾傅立叶变换(又回来了,唉),因为他们都是频率变换的方法,而傅立叶变换是最入门的,也是最先了解的, ...
- common-fileupload组件实现java文件上传和下载
简介:文件上传和下载是java web中常见的操作,文件上传主要是将文件通过IO流传放到服务器的某一个特定的文件夹下,而文件下载则是与文件上传相反,将文件从服务器的特定的文件夹下的文件通过IO流下载到 ...
- 文件系统inodes使用率过高问题处理
运维过程中经常碰见文件系统inodes使用率过高导致文件系统不可写的问题,常见场景如下 .Oracle产生的审计文件,特别是DG备库或者审计设置为OS时 .crontab产生大量邮件,导致/var/s ...
- 黑马基础阶段测试题:创建Phone(手机)类,Phone类中包含以下内容:
package com.swift; public class Phone { private String pinpai; private int dianliang; public String ...
- c++ 中常量与变量 基本数据类型
c++中常量如何分类? 1.整数常量,所有的整数. 整数又分为 int (integer) 占用4个字节 一个字节占几个二进制位?8个二进制位,一个整型变量占32位二进制位 (内存中开辟出来的存储空间 ...