WebWork2.2
工作原理总结
利用WebWork做的项目,在服务器启动时完成WebWork的框架初始化。具体是通过Web.xml中配置好的FilterDispatcher过滤器中的init(FilterConfig filterConfig)方法完成。并且web.xml中配置好FilterDispatcher的映射,当用户用映射好的结尾资源请求浏览器时,FillterDispather会进行请求处理.
一、WebWork的框架初始化过程
利用WebWork做的项目,在服务器启动时完成WebWork的框架初始化。具体是通过Web.xml中配置好的FilterDispatcher过滤器中的init(FilterConfig filterConfig)方法完成。并且web.xml中配置好FilterDispatcher的映射,当用户用映射好的结尾资源请求浏览器时,FillterDispather会进行请求处理.
具体实现是通过以下步骤:
1、 通过
FilterDispatcher
中的
public void init(FilterConfig filterConfig) throws ServletException
方法,进行框架的初始化
2、
Init
方法又同过调用
DispatcherUtils
类的public static void initialize(ServletContext servletContext)
方法创建DispatcherUtils
实例,同时间接调用
DispatcherUtils
类的protected void init(ServletContext servletContext)
方法初始化
Configuration
配置,创建对象创建的工厂
ObjectFactory
和
ObjectTypeDeterminer
。至此完成
WebWork
框架的初始化。
// 通过createActionInvocation 方法创建动作调用类 ActionInvocation ,处理被 Action 调用的方法
private void resolveMethod() {
二、WebWork的用户请求处理过程
所有以web.xml中映射FilterDispatcher结尾的服务请求将由FilterDispatcher进行处理。
1、从用户请求的服务名中解析出对应Action的名称。
1、从用户请求的服务名中解析出对应Action的名称。
具体完成是:户按webwork规则请求时,服务器会调用FilterDispatcher的
doFilter
方法,完成第二步的内容。
2、遍历 HttpServletRequest、HttpSession、ServletContext 中的数据,并将其复制到Webwork的Map中,为下一步创建Action事例打下基础。
具体完成是:过调用DispatcherUtils的
serviceAction
方法中的
Map extraContext = createContextMap(request, response, mapping, context);
完成以上信息的封装。
Map extraContext = createContextMap(request, response, mapping, context);
完成以上信息的封装。
3、以上一步封装好的信息为参数,调用 ActionProxyFactory创建对应的 ActionProxy实例。ActionProxyFactory 将根据 Xwork 配置文件(xwork.xml)中的设定,创建ActionProxy实例,ActionProxy中包含了 Action的配置信息(包括 Action名称,对应实现类等等)。
具体完成是:通过
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, name, extraContext, true, false);//创建动态代理
DefaultActionProxyFactory
实现ActionProxyFactory的createActionProxy方法,返回new DefaultActionProxy(namespace, actionName, extraContext, true, true);
DefaultActionProxy是对
ActionProxy
的默认实现,
通过DefaultActionProxy 类的DefaultActionProxy(namespace, actionName, extraContext, true, true)构造方法实例化DefaultActionProxy,同时得到用户请求的actionName及namespace,并通过
config
= ConfigurationManager.getConfiguration().getRuntimeConfiguration().getActionConfig(namespace, actionName);
ConfigurationManager
的
public
static
synchronized
Configuration getConfiguration() {
if
(
configurationInstance
==
null
) {
configurationInstance
=
new
DefaultConfiguration();
try
{
configurationInstance
.reload();
}
catch
(ConfigurationException e) {
configurationInstance
=
null
;
throw
e;
}
}
else
{
conditionalReload();
}
return
configurationInstance
;
}
完成对xwork.xml(具体操作类是XmlConfigurationProvider)配置信息的读取。
获得与此次请求相关的
ActionConfig
4、ActionProxy创建对应的Action实例,并根据配置进行一系列的处理程序。
通过
DefaultActionProxy
类的
invocation = ActionProxyFactory.getFactory().createActionInvocation(this, extraContext);
// 通过createActionInvocation 方法创建动作调用类 ActionInvocation ,处理被 Action 调用的方法
private void resolveMethod() {
// if the method is set to null, use the one from the configuration
// if the one from the configuration is also null, use "execute"
if
(!TextUtils.stringSet(
this
.
method
)) {
this
.
method
=
config
.getMethodName();
if
(!TextUtils.stringSet(
this
.
method
)) {
this
.
method
=
"execute"
;
}
}
}
然后调用
DispatcherUtils
的
serviceAction
方法中的
if
(mapping.getResult() !=
null
) {
Result result = mapping.getResult();
result.execute(proxy.getInvocation());
}
else
{
proxy.execute();
}
完成用户的最终要执行的
action
方法。
public
String execute()
throws
Exception {
ActionContext nestedContext = ActionContext.getContext();
ActionContext.setContext(
invocation
.getInvocationContext());
String retCode =
null
;
try
{
retCode =
invocation
.invoke();
}
finally
{
if
(
cleanupContext
) {
ActionContext.setContext(nestedContext);
}
}
return
retCode;
}
最终
处理
ActionContext
对象
将
Action
调用提交给
ActionInvocation
处理
三、WebWork的执行流程图
<v:shapetype coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f" id="_x0000_t75"> </v:shapetype>