Struts2(二)工作原理
一、概述
1、struts框架本身分为三个部分:核心控制器FilterDispatcher、业务控制器Action和用户实现的企业业务逻辑组件。
2、struts2工作的基本流程:
- 客户端初始化一个指向Servlet容器的请求
 - org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter被调用,该过滤器询问ActionMaper这个请求是否需要调用某个Action
 - 如果ActionMapper决定需要调用某个Action,StrutsPrepareAndExecuteFilter把请求的处理交给ActionProxy
 - ActionProxy通过Configuration Manager询问框架的配置文件,找到需要调用的Action类
 - ActionProxy创建一个ActionProxy的实例
 - ActionProxy实例使用命名模式来调用
 - 一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果
 
二、源码分析
下面解析org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter类
public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter
{
/*
* prepareOperation对象包含执行请求之前的准备工作
* ExecuteOperations对象包含过滤器的执行操作
*/
protected PrepareOperations prepare;
protected ExecuteOperations execute;
protected List<Pattern> excludedPatterns = null; public void init(FilterConfig filterConfig) throws ServletException
{
//InitOperations类包含一些初始化操作
InitOperations init = new InitOperations();
Dispatcher dispatcher = null;
try
{
//封装filterConfig,其中有个主要方法getInitParameterNames将参数名字以String格式存储在List中
FilterHostConfig config = new FilterHostConfig(filterConfig);
//初始化struts内部日志
init.initLogging(config);
//创建dispatcher ,并初始化
dispatcher = init.initDispatcher(config);
init.initStaticContentLoader(config, dispatcher);
//初始化类属性:prepare 、execute
prepare = new PrepareOperations(dispatcher);
execute = new ExecuteOperations(dispatcher);
this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
//回调空的postInit方法
postInit(dispatcher, filterConfig);
}
finally
{
if (dispatcher != null) {
dispatcher.cleanUpAfterInit();
}
init.cleanup();
}
}
/**
* Callback for post initialization
*/
protected void postInit(Dispatcher dispatcher, FilterConfig filterConfig)
{
} public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
try
{
if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns))
{
//过滤器放行
chain.doFilter(request, response);
}
else
{
//设置编码国际化和地点
prepare.setEncodingAndLocale(request, response);
//创建action上下文
//ActionContext是一个线程的本地变量,这意味着不同的action之间不会共享ActionContext,所以也不用考虑线程安全问题
prepare.createActionContext(request, response);
prepare.assignDispatcherToThread();
request = prepare.wrapRequest(request);
ActionMapping mapping = prepare.findActionMapping(request, response, true);
//如果mapping为空,则认为不是调用action,会调用下一个过滤器链,直到获取到mapping才调用action
if (mapping == null)
{
boolean handled = execute.executeStaticResourceRequest(request, response);
if (!handled)
{
chain.doFilter(request, response);
}
}
else
{
//执行action
execute.executeAction(request, response, mapping);
}
}
}
finally
{
prepare.cleanupRequest(request);
}
}
public void destroy() {
prepare.cleanupDispatcher();
}
}
上述源码的第23行:dispatcher = init.initDispatcher(config);
//创建并初始化Dispatcher对象
public Dispatcher initDispatcher( HostConfig filterConfig )
{
Dispatcher dispatcher = createDispatcher(filterConfig);
dispatcher.init();
return dispatcher;
}
创建dispatcher,会读取 filterConfig 中的配置信息,将配置信息解析出来,封装成为一个Map,然后根绝servlet上下文和参数Map构造Dispatcher :
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过程如下:
public void init()
{
if (configurationManager == null)
{
configurationManager = createConfigurationManager(DefaultBeanSelectionProvider.DEFAULT_BEAN_NAME);
}
//初始化过程中,会加载一些配置文件,例如:default.properties,struts-default.xml,struts-plugin.xml,struts.xml等
try
{
init_FileManager();
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_CheckWebLogicWorkaround(container);
if (!dispatcherListeners.isEmpty())
{
for (DispatcherListener l : dispatcherListeners)
{
l.dispatcherInitialized(this);
}
}
errorHandler.init(servletContext);
}
catch (Exception ex)
{
if (LOG.isErrorEnabled())
LOG.error("Dispatcher initialization failed", ex);
throw new StrutsException(ex);
}
}
上述分析的是StrutsPrepareAndExecuteFilter类的init方法,该方法在web容器启动的时候就会被调用,当用户访问某个action时,首先调用StrutsPrepareAndExecuteFilter类的doFilter方法,下面具体分析下这个方法:
- 首先是设置编码格式和地点:
 
prepare.setEncodingAndLocale(request, response);
- 创建ActionContext,ActionContext(com.opensymphony.xwork.ActionContext)是Action执行时的上下文,上下文可以看作是一个容器(其实我们这里的容器就是一个Map而已),它存放的是Action在执行时需要用到的对象,比如Session、Application、Request、Locale、ValueStack等。
 
prepare.createActionContext(request, response);
- 分配调度到本地线程调度
 
prepare.assignDispatcherToThread();
- request进行包装,如果content_type是multipart/form-data类型,则将request包装成MultiPartRequestWrapper对象,否则包装成StrutsRequestWrapper对象
 
request = prepare.wrapRequest(request);
public HttpServletRequest wrapRequest(HttpServletRequest request) throws IOException
{
// don't wrap more than once
if (request instanceof StrutsRequestWrapper)
{
return request;
}
String content_type = request.getContentType();
if (content_type != null && content_type.contains("multipart/form-data"))
{
MultiPartRequest mpr = getMultiPartRequest();
LocaleProvider provider = getContainer().getInstance(LocaleProvider.class);
request = new MultiPartRequestWrapper(mpr, request, getSaveDir(), provider, disableRequestAttributeValueStackLookup);
}
else
{
request = new StrutsRequestWrapper(request, disableRequestAttributeValueStackLookup);
}
return request;
}
- 然后通过ActionMapper的getMapping()方法得到请求的Action,Action的配置信息存储在ActionMapping对象中,
 
ActionMapping mapping = prepare.findActionMapping(request, response, true);
我们找到prepare对象的findActionMapping方法:
public ActionMapping findActionMapping(HttpServletRequest request, HttpServletResponse response, boolean forceLookup)
{
//首先从request对象中取mapping对象,看是否存在
ActionMapping mapping = (ActionMapping) request.getAttribute(STRUTS_ACTION_MAPPING_KEY); //struts.actionMapping
//不存在就创建一个
if (mapping == null || forceLookup)
{
try
{
//首先创建ActionMapper对象,通过ActionMapper对象创建mapping对象
mapping = dispatcher.getContainer().getInstance(ActionMapper.class).getMapping(request, dispatcher.getConfigurationManager());
if (mapping != null)
{
request.setAttribute(STRUTS_ACTION_MAPPING_KEY, mapping);
}
} catch (Exception ex) {
dispatcher.sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex);
}
}
return mapping;
}
ActionMapper接口的实现类DefaultActionMapper的getMapping()方法的源代码:
public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager)
{
ActionMapping mapping = new ActionMapping();
//获得请求的uri,即请求路径URL中工程名以后的部分,如/HelloWorld.action
String uri = getUri(request);
int indexOfSemicolon = uri.indexOf(";");
uri = (indexOfSemicolon > -1) ? uri.substring(0, indexOfSemicolon) : uri;
//删除扩展名,如.action或者.do
uri = dropExtension(uri, mapping);
if (uri == null)
{
return null;
}
//从uri中分离得到请求的action名、命名空间。
parseNameAndNamespace(uri, mapping, configManager);
//处理特殊的请求参数
handleSpecialParameters(request, mapping);
//如果允许动态方法调用,即形如/HelloWorldAction!getAll.action的请求,分离action名和方法名
return parseActionName(mapping);
}
- 如果mapping为空,则认为不是调用action,会调用下一个过滤器链,直到获取到mapping才调用action
 
if (mapping == null)
{
boolean handled = execute.executeStaticResourceRequest(request, response);
if (!handled)
{
chain.doFilter(request, response);
}
}
- 如果mapping对象不为空,则会执行action
 
execute.executeAction(request, response, mapping);
其源码为:
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException
{
//封转上下文环境,主要将requestMap、params、session等Map封装成为一个上下文Map
Map<String, Object> extraContext = createContextMap(request, response, mapping);
// If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
boolean nullStack = stack == null;
if (nullStack)
{
ActionContext ctx = ActionContext.getContext();
if (ctx != null)
{
stack = ctx.getValueStack();
}
}
if (stack != null)
{
extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
}
String timerKey = "Handling request from Dispatcher";
try
{
UtilTimerStack.push(timerKey);
String namespace = mapping.getNamespace();//从mapping对象获取命名空间
String name = mapping.getName(); //获取请求的action名
String method = mapping.getMethod(); //获取请求方法 //根据执行上下文参数,命名空间,名称等创建用户自定义Action的代理对象
ActionProxy proxy = getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
namespace, name, method, extraContext, true, false); request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack()); //如果配置文件中执行的这个action配置了result,就直接转到result
if (mapping.getResult() != null)
{
Result result = mapping.getResult();
result.execute(proxy.getInvocation());
}
else
{
proxy.execute();
} // If there was a previous value stack then set it back onto the request
if (!nullStack) {
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
}
}
catch (ConfigurationException e)
{
logConfigurationException(request, e);
sendError(request, response, HttpServletResponse.SC_NOT_FOUND, e);
}
catch (Exception e)
{
if (handleException || devMode)
{
sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
}
else
{
throw new ServletException(e);
}
}
finally
{
UtilTimerStack.pop(timerKey);
}
}
}
- 最后通过Result完成页面跳转。
 
Struts2(二)工作原理的更多相关文章
- Struts2的工作原理及工作流程
		
众所周知,Struts2是个非常优秀的开源框架,我们能用Struts2框架进行开发,同时能 快速搭建好一个Struts2框架,但我们是否能把Struts2框架的工作原理用语言表达清楚,你表达的原理不需 ...
 - 菜鸟学Struts2——Struts工作原理
		
在完成Struts2的HelloWorld后,对Struts2的工作原理进行学习.Struts2框架可以按照模块来划分为Servlet Filters,Struts核心模块,拦截器和用户实现部分,其中 ...
 - Struts2的工作原理(图解)详解
		
Struts2的工作原理 上图来源于Struts2官方站点,是Struts 2 的整体结构. 一个请求在Struts2框架中的处理大概分为以下几个步骤(可查看源码:https://github.com ...
 - Struts2 的工作原理
		
Struts2 的工作原理: 1)client向server发出一个http请求.webserver对请求进行解析,假设在StrutsPrepareAndExecuteFilter的请求映射路径(在w ...
 - struts2的工作原理
		
在学习struts2就必须的了解一下它的工作原理: 首先来看一下这张图 这张工作原理图是官方提供的: 一个请求在Struts2框架中的处理大概分为以下几个步骤 1.客户端初始化一个指向Servlet容 ...
 - 第一篇——Struts2的工作原理及HelloWorld简单实现
		
Struts2工作原理: 一个请求在Struts框架中的处理步骤: 1.客户端初始化一个指向Servlet容器(例如Tomcat)的请求: 2.这个请求经过一系列的过滤器(Filter): 3.接着F ...
 - Struts2学习一----------Struts2的工作原理及HelloWorld简单实现
		
© 版权声明:本文为博主原创文章,转载请注明出处 Struts2工作原理 一个请求在Struts2框架中的处理步骤: 1.客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2.这个请求 ...
 - HDSF主要节点解说(二)工作原理
		
HDFS(Hadoop Distributed File System )Hadoop分布式文件系统. 是依据google发表的论文翻版的.论文为GFS(Google File System)Goog ...
 - 分享知识-快乐自己:Struts2框架 工作原理及执行流程图(拦截器的使用)
		
Struts2 架构图: 1):提交请求 客户端通过 HttpServletRequest 向 Servlet (即Tomcat)提交一个请求. 请求经过一系列的过滤器,例如图中的 ActionCon ...
 
随机推荐
- qualcomm lk gpio
			
关于高通平台lk中控制gpio的记录 http://blog.csdn.net/loongembedded/article/details/72834761 http://blog.csdn.net/ ...
 - Kilo 版 Keystone 数据库结构
			
在安装完keystone并利用keystone-manage命令同步数据库后,mysql(我使用的存储后端)中新加了如下表: +------------------------+ | Tables_i ...
 - 免费在线直播课,送给所有IT项目经理
			
[免费在线直播课,送给所有IT项目经理]项目管理培训领域的老资格——光环国际,精心策划了一门一个半小时的在线直播课,送给所有辛苦的IT项目经理们.[直播主题]变化时代IT项目经理的成长要求[直播内容 ...
 - 每天一个linux命令:mkdir命令
			
linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1.命令格式: mkdir [选项] 目录... 2.命令 ...
 - php 多页面间共享变量
			
1. 保存session的页面session1.php <? echo "hi, let's try session<br/>"; session_start() ...
 - Java SE 8 并发增强
			
1.原子值 java5开始,提供了一些原子操作的类,如AtomicInteger.AtomicLong等 这些类提供了诸如incrementAndGet这样的原子操作方法. 单数如果想进行复杂操作,则 ...
 - iOS :UIPickerView reloadAllComponets not work
			
编辑信息页面用了很多选择栏,大部分都用 UIPickerView 来实现.在切换数据显示的时候, UIPickerView 不更新数据,不得其解.Google 无解,原因在于无法描述自己的问题,想想应 ...
 - jquery click事件,多次执行
			
用jquery绑定一个按钮click事件后,第一次点击后,一切正常,第二次点击,竟然执行两次,以后越来越多, 后来查看文档发现 jquery click 不是 替换原有的function 而是接 ...
 - node.js--Less
			
摘要: 现在已经有许多站点使用Node.js,所以在Node.js上配置Less环境也是很重要的,下面分享下如何在Node上使用Less开发,前提是你电脑上已经安装node. 安装: 只需要执行下面一 ...
 - HTML5标签canvas图像处理
			
摘要: canvas可以读取图片后,使用drawImage方法在画布内进行重绘.本文介绍canvas的图像处理 drawImage drawImage() 方法在画布上绘制图像.画布或视频.drawI ...