struts2拦截器源码分析
前面博客我们介绍了开发struts2应用程序的基本流程(开发一个struts2的实例),通过前面我们知道了struts2实现请求转发和配置文件加载都是拦截器进行的操作,这也就是为什么我们要在web.xml配置struts2的拦截器的原因了。我们知道,在开发struts2应用开发的时候我们要在web.xml进行配置拦截器org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter(在一些老版的一般配置org.apache.struts2.dispatcher.FilterDispatcher),不知道大家刚开始学的时候有没有这个疑问,为什么通过这个拦截器我们就可以拦截到我们提交的请求,并且一些配置文件就可以得到加载呢?不管你有没有,反正我是有。我想这个问题的答案,我们是非常有必要去看一下这个拦截器的源码去找。
打开StrutsPrepareAndExecuteFilter拦截器源码我们可以看出以下类的信息
属性摘要:
Protected List<Pattern> excludedPatterns
protected ExecuteOperations execute
protected PrepareOperations prepare
我们可以看出StrutsPrepareAndExecuteFilter与普通的Filter并无区别,方法除继承自Filter外,仅有一个回调方法,第三部分我们将按照Filter方法调用顺序,init—>doFilter—>destroy顺序地分析源码。
提供的方法:
destroy() |
继承自Filter,用于资源释放 |
doFilter(ServletRequest req, ServletResponse res, FilterChain chain) |
继承自Filter,执行方法 |
init(FilterConfig filterConfig) |
继承自Filter,初始化参数 |
postInit(Dispatcher dispatcher, FilterConfig filterConfig) |
Callback for post initialization(一个空的方法,用于方法回调初始化) |
下面我们一一对这些方法看一下:
1.init方法:我们先整体看一下这个方法:
[java] view plaincopyprint?
public void init(FilterConfig filterConfig) throws ServletException {
InitOperations init = new InitOperations();
try {
//封装filterConfig,其中有个主要方法getInitParameterNames将参数名字以String格式存储在List中
FilterHostConfig config = new FilterHostConfig(filterConfig);
// 初始化struts内部日志
init.initLogging(config);
//创建dispatcher ,并初始化,这部分下面我们重点分析,初始化时加载那些资源
Dispatcher dispatcher = init.initDispatcher(config);
init.initStaticContentLoader(config, dispatcher);
//初始化类属性:prepare 、execute
prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
//回调空的postInit方法
postInit(dispatcher, filterConfig);
} finally {
init.cleanup();
}
}
首先开一下FilterHostConfig 这个封装configfilter的类: 这个类总共不超过二三十行代码getInitParameterNames是这个类的核心,将Filter初始化参数名称有枚举类型转为Iterator。此类的主要作为是对filterConfig 封装。具体代码如下:
[java] view plaincopyprint?
public Iterator<String> getInitParameterNames() {
return MakeIterator.convert(config.getInitParameterNames());
}
下面咱接着一块看Dispatcher dispatcher = init.initDispatcher(config);这是重点,创建并初始化Dispatcher ,看一下具体代码:
[html] view plaincopyprint?
public Dispatcher initDispatcher( HostConfig filterConfig ) {
Dispatcher dispatcher = createDispatcher(filterConfig);
dispatcher.init();
return dispatcher;
}
span style="FONT-SIZE: 18px"><span style="color:#000000;BACKGROUND: rgb(255,255,255)"> </span><span style="color:#000000;BACKGROUND: rgb(255,255,255)"><span style="color:#cc0000;"><strong>创建<span style="font-family:Verdana;">Dispatcher</span><span style="font-family:宋体;">,会读取 </span><span style="font-family:Verdana;">filterConfig </span><span style="font-family:宋体;">中的配置信息,将配置信息解析出来,封装成为一个</span><span style="font-family:Verdana;">Map</span></strong></span><span style="font-family:宋体;">,然后</span></span><span style="color:#000000;BACKGROUND: rgb(255,255,255)">根据</span><span style="color:#000000;BACKGROUND: rgb(255,255,255)">servlet<span style="font-family:宋体;">上下文和参数</span><span style="font-family:Verdana;">Map</span><span style="font-family:宋体;">构造</span><span style="font-family:Verdana;">Dispatcher </span><span style="font-family:宋体;">:</span></span></span>
[java] view plaincopyprint?
private Dispatcher createDispatcher( HostConfig filterConfig ) {
Map<String, String> params = new HashMap<String, String>();
for ( Iterator e = filterConfig.getInitParameterNames(); e.hasNext(); ) {
String name = (String) e.next();
String value = filterConfig.getInitParameter(name);
params.put(name, value);
}
return new Dispatcher(filterConfig.getServletContext(), params);
}
Dispatcher构造玩以后,开始对他进行初始化,加载struts2的相关配置文件,将按照顺序逐一加载:default.properties,struts-default.xml,struts-plugin.xml,struts.xml,……我们一起看看他是怎么一步步的加载这些文件的 dispatcher的init()方法:
[html] view plaincopyprint?
public void init() {
if (configurationManager == null) {
configurationManager = createConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);
}
try {
init_DefaultProperties(); // [1]
init_TraditionalXmlConfigurations(); // [2]
init_LegacyStrutsProperties(); // [3]
init_CustomConfigurationProviders(); // [5]
init_FilterInitParameters() ; // [6]
init_AliasStandardObjects() ; // [7]
Container container = init_PreloadConfiguration();
container.inject(this);
init_CheckConfigurationReloading(container);
init_CheckWebLogicWorkaround(container);
if (!dispatcherListeners.isEmpty()) {
for (DispatcherListener l : dispatcherListeners) {
l.dispatcherInitialized(this);
}
}
} catch (Exception ex) {
if (LOG.isErrorEnabled())
LOG.error("Dispatcher initialization failed", ex);
throw new StrutsException(ex);
}
}
下面我们一起来看一下【1】,【2】,【3】,【5】,【6】的源码,看一下什么都一目了然了:
1.这个方法中是将一个DefaultPropertiesProvider对象追加到ConfigurationManager对象内部的ConfigurationProvider队列中。 DefaultPropertiesProvider的register()方法可以载入org/apache/struts2/default.properties中定义的属性。
[html] view plaincopyprint?
try {
defaultSettings = new PropertiesSettings("org/apache/struts2/default");
} catch (Exception e) {
throw new ConfigurationException("Could not find or error in org/apache/struts2/default.properties", e);
}
2. 调用init_TraditionalXmlConfigurations()方法,实现载入FilterDispatcher的配置中所定义的config属性。 如果用户没有定义config属性,struts默认会载入DEFAULT_CONFIGURATION_PATHS这个值所代表的xml文件。它的值为"struts-default.xml,struts-plugin.xml,struts.xml"。也就是说框架默认会载入这三个项目xml文件。如果文件类型是XML格式,则按照xwork-x.x.dtd模板进行读取。如果,是Struts的配置文件,则按struts-2.X.dtd模板进行读取。
private static final String DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml";
3.创建一个LegacyPropertiesConfigurationProvider类,并将它追加到ConfigurationManager对象内部的ConfigurationProvider队列中。LegacyPropertiesConfigurationProvider类载入struts.properties中的配置,这个文件中的配置可以覆盖default.properties中的。其子类是DefaultPropertiesProvider类
5.init_CustomConfigurationProviders()此方法处理的是FilterDispatcher的配置中所定义的configProviders属性。负责载入用户自定义的ConfigurationProvider。
[html] view plaincopyprint?
String configProvs = initParams.get("configProviders");
if (configProvs != null) {
String[] classes = configProvs.split("\\s*[,]\\s*");
for (String cname : classes) {
try {
Class cls = ClassLoaderUtils.loadClass(cname, this.getClass());
ConfigurationProvider prov = (ConfigurationProvider)cls.newInstance();
configurationManager.addConfigurationProvider(prov);
} catch (InstantiationException e) {
throw new ConfigurationException("Unable to instantiate provider: "+cname, e);
} catch (IllegalAccessException e) {
throw new ConfigurationException("Unable to access provider: "+cname, e);
} catch (ClassNotFoundException e) {
throw new ConfigurationException("Unable to locate provider class: "+cname, e);
}
}
}
6.init_FilterInitParameters()此方法用来处理FilterDispatcher的配置中所定义的所有属性
7. init_AliasStandardObjects(),将一个BeanSelectionProvider类追加到ConfigurationManager对象内部的ConfigurationProvider队列中。BeanSelectionProvider类主要实现加载org/apache/struts2/struts-messages。
[html] view plaincopyprint?
private void init_AliasStandardObjects() {
configurationManager.addConfigurationProvider(
new BeanSelectionProvider());
}
相信看到这大家应该明白了,struts2的一些配置的加载顺序和加载时所做的工作,其实有些地方我也不是理解的很清楚。其他具体的就不在说了,init方法占时先介绍到这
2、doFilter方法
doFilter是过滤器的执行方法,它拦截提交的HttpServletRequest请求,HttpServletResponse响应,作为strtus2的核心拦截器,在doFilter里面到底做了哪些工作,我们将逐行解读其源码,大体源码如下:
[html] view plaincopyprint?
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
//父类向子类转:强转为http请求、响应
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
try {
//设置编码和国际化
prepare.setEncodingAndLocale(request, response);
//创建Action上下文(重点)
prepare.createActionContext(request, response);
prepare.assignDispatcherToThread();
if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
chain.doFilter(request, response);
} else {
request = prepare.wrapRequest(request);
ActionMapping mapping = prepare.findActionMapping(request, response, true);
if (mapping == null) {
boolean handled = execute.executeStaticResourceRequest(request, response);
if (!handled) {
chain.doFilter(request, response);
}
} else {
execute.executeAction(request, response, mapping);
}
}
} finally {
prepare.cleanupRequest(request);
}
}
下面我们就逐句的来的看一下:设置字符编码和国际化很简单prepare调用了setEncodingAndLocale,然后调用了dispatcher方法的prepare方法:
[html] view plaincopyprint?
/**
* Sets the request encoding and locale on the response
*/
public void setEncodingAndLocale(HttpServletRequest request, HttpServletResponse response) {
dispatcher.prepare(request, response);
}
看下prepare方法,这个方法很简单只是设置了encoding 、locale ,做的只是一些辅助的工作:
[html] view plaincopyprint?
public void prepare(HttpServletRequest request, HttpServletResponse response) {
String encoding = null;
if (defaultEncoding != null) {
encoding = defaultEncoding;
}
Locale locale = null;
if (defaultLocale != null) {
locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());
}
if (encoding != null) {
try {
request.setCharacterEncoding(encoding);
} catch (Exception e) {
LOG.error("Error setting character encoding to '" + encoding + "' - ignoring.", e);
}
}
if (locale != null) {
response.setLocale(locale);
}
if (paramsWorkaroundEnabled) {
request.getParameter("foo"); // simply read any parameter (existing or not) to "prime" the request
}
}
下面咱重点看一下创建Action上下文重点
Action上下文创建(重点)
ActionContext是一个容器,这个容易主要存储request、session、application、parameters等相关信息.ActionContext是一个线程的本地变量,这意味着不同的action之间不会共享ActionContext,所以也不用考虑线程安全问题。其实质是一个Map,key是标示request、session、……的字符串,值是其对应的对象:
static ThreadLocal actionContext = new ThreadLocal();
Map<String, Object> context;
下面我们看起来下创建action上下文的源码:
[html] view plaincopyprint?
/**
*创建Action上下文,初始化thread local
*/
public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) {
ActionContext ctx;
Integer counter = 1;
Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
if (oldCounter != null) {
counter = oldCounter + 1;
}
//注意此处是从ThreadLocal中获取此ActionContext变量
ActionContext oldContext = ActionContext.getContext();
if (oldContext != null) {
// detected existing context, so we are probably in a forward
ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap()));
} else {
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));
//stack.getContext()返回的是一个Map<String,Object>,根据此Map构造一个ActionContext
ctx = new ActionContext(stack.getContext());
}
request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
//将ActionContext存如ThreadLocal
ActionContext.setContext(ctx);
return ctx;
}
一句句来看:
ValueStackstack= dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
dispatcher.getContainer().getInstance(ValueStackFactory.class)根据字面估计一下就是创建ValueStackFactory的实例。这个地方我也只是根据字面来理解的。ValueStackFactory是接口,其默认实现是OgnlValueStackFactory,调用OgnlValueStackFactory的createValueStack():
下面看一下OgnlValueStack的构造方法
[html] view plaincopyprint?
protected OgnlValueStack(XWorkConverter xworkConverter, CompoundRootAccessor accessor, TextProvider prov, boolean allowStaticAccess) {
//new一个CompoundRoot出来
setRoot(xworkConverter, accessor, new CompoundRoot(), allowStaticAccess);
push(prov);
}
接下来看一下setRoot方法:
[html] view plaincopyprint?
protected void setRoot(XWorkConverter xworkConverter, CompoundRootAccessor accessor, CompoundRoot compoundRoot,
boolean allowStaticMethodAccess) {
//OgnlValueStack.root = compoundRoot;
this.root = compoundRoot;
1 //方法/属性访问策略。
this.securityMemberAccess = new SecurityMemberAccess(allowStaticMethodAccess);
//创建context了,创建context使用的是ongl的默认方式。
//Ognl.createDefaultContext返回一个OgnlContext类型的实例
//这个OgnlContext里面,root是OgnlValueStack中的compoundRoot,map是OgnlContext自己创建的private Map _values = new HashMap(23);
this.context = Ognl.createDefaultContext(this.root, accessor, new OgnlTypeConverterWrapper(xworkConverter), securityMemberAccess);
//不是太理解,猜测如下:
//context是刚刚创建的OgnlContext,其中的HashMap类型_values加入如下k-v:
//key:com.opensymphony.xwork2.util.ValueStack.ValueStack
//value:this,这个应该是当前的OgnlValueStack实例。
//刚刚用断点跟了一下,_values里面是:
//com.opensymphony.xwork2.ActionContext.container=com.opensymphony.xwork2.inject.ContainerImpl@96231e
//com.opensymphony.xwork2.util.ValueStack.ValueStack=com.opensymphony.xwork2.ognl.OgnlValueStack@4d912
context.put(VALUE_STACK, this);
//此时:OgnlValueStack中的compoundRoot是空的;
//context是一个OgnlContext,其中的_root指向OgnlValueStack中的root,_values里面的东西,如刚才所述。
//OgnlContext中的额外设置。
Ognl.setClassResolver(context, accessor);
((OgnlContext) context).setTraceEvaluations(false);
((OgnlContext) context).setKeepLastEvaluation(false);
}
上面代码中dispatcher.createContextMap,如何封装相关参数:,我们以RequestMap为例,其他的原理都一样:主要方法实现:
[html] view plaincopyprint?
//map的get实现
public Object get(Object key) {
return request.getAttribute(key.toString());
}
//map的put实现
public Object put(Object key, Object value) {
Object oldValue = get(key);
entries = null;
request.setAttribute(key.toString(), value);
return oldValue;
}
到此,几乎StrutsPrepareAndExecuteFilter大部分的源码都涉及到了。自己感觉都好乱,所以还请大家见谅,能力有限,希望大家可以共同学习
本文来自:曹胜欢博客专栏。转载请注明出处:http://blog.csdn.net/csh624366188
struts2拦截器源码分析的更多相关文章
- [原创]java WEB学习笔记70:Struts2 学习之路-- struts2拦截器源码分析,运行流程
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- OkHttp3 拦截器源码分析
OkHttp 拦截器流程源码分析 在这篇博客 OkHttp3 拦截器(Interceptor) ,我们已经介绍了拦截器的作用,拦截器是 OkHttp 提供的对 Http 请求和响应进行统一处理的强大机 ...
- Jfinal拦截器源码解读
本文对Jfinal拦截器源码做以下分析说明
- linux调度器源码分析 - 运行(四)
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 引言 之前的文章已经将调度器的数据结构.初始化.加入进程都进行了分析,这篇文章将主要说明调度器是如何在程序稳定运 ...
- linux调度器源码分析 - 初始化(二)
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 引言 上期文章linux调度器源码分析 - 概述(一)已经把调度器相关的数据结构介绍了一遍,本篇着重通过代码说明 ...
- 一步步实现windows版ijkplayer系列文章之三——Ijkplayer播放器源码分析之音视频输出——音频篇
一步步实现windows版ijkplayer系列文章之一--Windows10平台编译ffmpeg 4.0.2,生成ffplay 一步步实现windows版ijkplayer系列文章之二--Ijkpl ...
- 一步步实现windows版ijkplayer系列文章之二——Ijkplayer播放器源码分析之音视频输出——视频篇
一步步实现windows版ijkplayer系列文章之一--Windows10平台编译ffmpeg 4.0.2,生成ffplay 一步步实现windows版ijkplayer系列文章之二--Ijkpl ...
- Linux 内核调度器源码分析 - 初始化
导语 上篇系列文 混部之殇-论云原生资源隔离技术之CPU隔离(一) 介绍了云原生混部场景中CPU资源隔离核心技术:内核调度器,本系列文章<Linux内核调度器源码分析>将从源码的角度剖析内 ...
- 0002 - Spring MVC 拦截器源码简析:拦截器加载与执行
1.概述 Spring MVC中的拦截器(Interceptor)类似于Servlet中的过滤器(Filter),它主要用于拦截用户请求并作相应的处理.例如通过拦截器可以进行权限验证.记录请求信息的日 ...
随机推荐
- 【转】iOS 硬件授权检测:定位服务、通讯录、日历、提醒事项、照片、蓝牙共享、麦克风、相机等
iOS系统版本的不断升级的前提,伴随着用户使用设备的安全性提升,iOS系统对于App需要使用的硬件限制也越来越严格,App处理稍有不妥,轻则造成功能不可用用户还不知道,重则会造成App Crash. ...
- flask前后台交互数据的几个思路
通过url进行参数传递: @app.route('/hello/<name>') # <name>为传递的参数 def hello(name=None): return ren ...
- CodeForces 589B Layer Cake (暴力)
题意:给定 n 个矩形是a*b的,问你把每一块都分成一样的,然后全放一块,高度都是1,体积最大是多少. 析:这个题,当时并没有完全读懂题意,而且也不怎么会做,没想到就是一个暴力,先排序,先从大的开始选 ...
- 【Java】IO技术的使用——用IO实现大文件的复制
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827481.html 用IO进行文件复制,实质就是用FileInputStream链接要复制的文件,按一定规模 ...
- MVC神韵---你想在哪解脱!(十一)
为了实现这一处理,我们需要在MoviesController类中追加第二个Create方法.这个Create方法具有一个[HttpPost]属性,它意味着我们将要用它来处理提交到“/Movies/Cr ...
- Linux下通过JDBC连接Oracle,SqlServer和PostgreSQL
今天正好需要统计三个网站栏目信息更新情况,而这三个网站的后台采用了不同的数据库管理系统.初步想法是通过建立一个小的Tomcat webapp,进而通过JDBC访问这三个后台数据库,并根据返回的数据生成 ...
- 通过克隆MAC地址 破解网通电信封路由
通过克隆MAC地址 破解网通电信封路由 电信封路由方法一:先确定申请上网的电脑单机状态下已经能够上网.就说明该电脑网卡的MAC地址是合法的MAC地址.进入系统的MSDOS方式,发布ipconfig/a ...
- java中hashcode和equals的区别和联系
HashSet和HashMap一直都是JDK中最常用的两个类,HashSet要求不能存储相同的对象,HashMap要求不能存储相同的键. 那么Java运行时环境是如何判断HashSet中相同对象.Ha ...
- Java中throws和throw的区别讲解
当然,你需要明白异常在Java中式以一个对象来看待.并且所有系统定义的编译和运行异常都可以由系统自动抛出,称为标准异常,但是一般情况下Java 强烈地要求应用程序进行完整的异常处理,给用户友好的提示, ...
- .net抓取网页信息 - Jumony框架使用1
往往在实际开发中,经常会用到一些如抓取网站信息之类的的操作,往往大家采用的是用一些正则的方式获取,但是有时候正则是很死板的,我们常常试想能不能使用jquery的选择器,获取符合自己要求的元素,然后进行 ...