前言

最近老大让每周写一篇技术性的博客,想想也没啥写,就想着随便拿个以前的项目去研究研究五大框架的底层代码。本人水平有限,有不对的地方还望大家勿喷,指正!

开始之前先了解下strtus2的工作流程:

工作原理图:



(1) 客户端(Client)向Action发用一个请求(Request)

(2) Container通过web.xml映射请求,并获得控制器(Controller)的名字

(3) 容器(Container)调用控制器(StrutsPrepareAndExecuteFilter或FilterDispatcher)。在Struts2.1以前调用FilterDispatcher,Struts2.1以后调用StrutsPrepareAndExecuteFilter

(4) 控制器(Controller)通过ActionMapper获得Action的信息

(5) 控制器(Controller)调用ActionProxy

(6) ActionProxy读取struts.xml文件获取action和interceptor stack的信息。

(7) ActionProxy把request请求传递给ActionInvocation

(8) ActionInvocation依次调用action和interceptor

(9) 根据action的配置信息,产生result

(10) Result信息返回给ActionInvocation

(11) 产生一个HttpServletResponse响应

(12) 产生的响应行为发送给客服端。

拦截器所涉及的接口和类:

用struts2实现拦截器有三种方式:

   1.实现Interceptor接口

	public interface Interceptor
extends Serializable
//继承Serializable
{
public abstract void destroy();
public abstract void init();
//ActionInvocation 是一个接口
public abstract String intercept(ActionInvocation actioninvocation)
throws Exception;
}

2.继承AbstractInterceptor类

	public abstract class AbstractInterceptor
implements Interceptor
//并没有具体实现
{
public AbstractInterceptor()
{
}
public void init()
{
}
public void destroy()
{
}
public abstract String intercept(ActionInvocation actioninvocation)
throws Exception;
}

所以我们并不建议用上面那两种方法

  1. 继承MethodFilterInterceptor类
       public abstract class MethodFilterInterceptor extends AbstractInterceptor
{
public MethodFilterInterceptor()
{
log = LoggerFactory.getLogger(getClass());
excludeMethods = Collections.emptySet();
includeMethods = Collections.emptySet();
}
public void setExcludeMethods(String excludeMethods)
{
this.excludeMethods = TextParseUtil.commaDelimitedStringToSet(excludeMethods);
}
public Set getExcludeMethodsSet()
{
return excludeMethods;
}
public void setIncludeMethods(String includeMethods)
{
this.includeMethods = TextParseUtil.commaDelimitedStringToSet(includeMethods);
}
public Set getIncludeMethodsSet()
{
return includeMethods;
}
public String intercept(ActionInvocation invocation)
throws Exception
{
if(applyInterceptor(invocation))
return doIntercept(invocation);
else
return invocation.invoke();
}
protected boolean applyInterceptor(ActionInvocation invocation)
{
//ActionInvocation将Web页面中的输入元素封装为一个(请求)数据对象”,这个对象就是ActionInvocation类型.
String method = invocation.getProxy().getMethod();
boolean applyMethod = MethodFilterInterceptorUtil.applyMethod(excludeMethods, includeMethods, method);
if(log.isDebugEnabled() && !applyMethod)
log.debug((new StringBuilder()).append("Skipping Interceptor... Method [").append(method).append("] found in exclude list.").toString(), new String[0]);
return applyMethod;
}
protected abstract String doIntercept(ActionInvocation actioninvocation)
throws Exception;
protected transient Logger log;
//排除的方法集合
protected Set excludeMethods;
//包括的方法集合(就是要拦截的方法)
protected Set includeMethods;
}

如上图:

在执行Action的前后都有拦截器的执行,每个拦截器类的doIntercept(ActionInvocation actionInvocation)方法都会传入一个参数ActionInvocation actionInvocation并且最后一句代码都是return invocation.invoke(),这句代码调用的是DefaultActionInvocation类的invoke方法,而在这个方法里面又会去调用其他的拦截器,这样的话就形成了一个类似递归的递归调用。





上面的这两张图说明了用拦截器时配置文件的基本配法。

下面用debug的方式跟下代码:

总结

Struts2拦截器执行机理如下:

  1. 整个结构就如同一个堆栈,除了Action以外,堆栈中的其他元素是Interceptor

  2. Action位于堆栈的底部。由于堆栈"先进后出"的特性,如果我们试图把Action拿出来执行,我们必须首先把位于Action上端的Interceptor拿出来执行。这样,整个执行就形成了一个递归调用

  3. 每个位于堆栈中的Interceptor,除了需要完成它自身的逻辑,还需要完成一个特殊的执行职责。这个执行职责有3种选择:

  1. 中止整个执行,直接返回一个字符串作为resultCode

  2. 通过递归调用负责调用堆栈中下一个Interceptor的执行

  3. 如果在堆栈内已经不存在任何的Interceptor,调用Action

struts2拦截器的实现机制的更多相关文章

  1. 【代码总结】Struts2 拦截器的处理机制

    一.什么是拦截器 拦截器是一个类,可以在Action被调用之前和之后执行代码,通常框架的很多功能都是拦截器实现的,如接收输入的参数.数据验证.文件上传等 二.工作方式 做一些Action执行前的预处理 ...

  2. Struts2拦截器模拟

    前言: 接触Struts2已经有一段时间,Student核心内容就是通过拦截器对接Action,实现View层的控制跳转.本文根据自身理解对Struts2进行一个Java实例的模拟,方便大家理解! 示 ...

  3. 浅谈Struts2拦截器的原理与实现

    拦截器与过滤器           拦截器是对调用的Action起作用,它提供了一种机制可以使开发者定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行.同时也是提供了 ...

  4. 基于SSH2框架Struts2拦截器的登录验证实现(转)

        大象在这里假设你已经弄清楚了Struts2拦截器的基本概念,可以进入实际运用了.那么我们在之前的基础上只需要做下小小的改变,就可以使用Struts2的拦截器机制实现登录的验证.     修改数 ...

  5. Struts2拦截器原理以及实例

    一.Struts2拦截器定义 1. Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现. 2. ...

  6. struts2(五)之struts2拦截器与自定义拦截器

    前言 前面介绍了struts2的输入验证,如果让我自己选的话,肯定是选择xml配置校验的方法,因为,能使用struts2中的一些校验规则,就无需自己编写了, 不过到后面应该都有其他更方便的校验方法,而 ...

  7. Struts2拦截器详解

    一.Struts2拦截器原理: Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts 2会查找配置文件,并根据其配置实例化相对的    拦截器对象,然后串成一个列 ...

  8. 解决Struts2拦截器的对于参数传递无效问题

    今天做项目时,使用拦截器对用户权限检查.拦截器本身没有问题,可是实现权限拦截,但是传递的参数全部都无效了.搞了很久,由于对拦截器的内部机制不是特别熟悉,所以重新研读了一下Struts2的拦截器.找到了 ...

  9. Struts2 拦截器配置以及实现

    @(Java ThirdParty)[Struts|Interceptor] Struts2 拦截器配置以及实现 Struts2的拦截器应用于Action,可以在执行Action的方法之前,之后或者两 ...

随机推荐

  1. Dikstra 堆优化板子

    #include <bits/stdc++.h> #define MAXN 10005 using namespace std; typedef long long LL; vector& ...

  2. Qt for Android使用grpc探索

    利用Qt在Android上使用grpc需要*.a的静态库,Windows上编译的lib库以及linux编译出来的.a,经过尝试,均无法链接成功.本文尝试使用NDK来编译Android版本的grpc静态 ...

  3. python3使用模块

    Python内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env python3 # -*- codi ...

  4. 你所不知的 PHP 断言(assert)

    PHP 中的断言常用于调试,检查一个表达式或语句是否为 FALSE.本文带你重新认识 PHP assert() 函数的神(Qi)通(Yin)广(Ji)大(Qiao). 本文基于 PHP Version ...

  5. Docker入门与简单使用

    前言:  Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的Linux或Windows机器上.近几年来,Docker 在国内发展的如 ...

  6. iptables 学习笔记

    1. 安装iptables yum install iptables centos7: yum install -y iptables-services 2. service命令 查看iptables ...

  7. C语言之整除

    除法运算符:/ 当除数和被除数都整形时,就是整除. 当浮点数和整数放到一起运算时,C语言会将整数转换成浮点数,然后进行浮点数的运算. #include<stdio.h> int main( ...

  8. Win10更新后wireshark无法获取网络接口

    一不小心win10自动更新了,打开wireshark发现它无法发现本地的网络接口. 其实解决的办法很简单,就是卸载npcap,安装Win10Pcap即可解决.

  9. logging模块、json序列化数据类型、单例、exception和baseException

    1.logging模块的作用以及应用场景 https://www.cnblogs.com/petrolero/p/9788134.html 2.json序列化可以处理的数据类型有哪些,如何定制支持da ...

  10. C++17尝鲜

    https://cloud.tencent.com/developer/article/1351910 [译]C++17,optional, any, 和 variant 的更多细节 用户261520 ...