写文章是一件困难的事,如果要讲的简单明了更难。
一,总体说明
来看看com.opensymphony.webwork.dispatcher.ServletDispatcher extends HttpServlet implements WebWorkStatics
看看也许是作者提的问题:
/*
* TODO: QUESTIONS:
*
* 1) What unit is maxSize of attachments in? (assuming bytes for now)
* 2) Isn't error message wrong in catch of try/catch in service() method?
* 3) Why is getActionName(String) not declared public? (The fix would not be an API addition so this could be
* done for pre 2.1)
* 4) Why does createContextMap(...) return a HashMap and not a Map? (2.1 api change)
* 5) Why doesn't getNameSpace(request) get the servlet path in the same way that getActionName(request) does?
* 6) Why does getParameterMap throw an IOException? Can't see a reason for that. (2.1 api change)
*/
注意 com.opensymphony.webwork.WebWorkStatics 一些webwork常量而己。把常量定义在interface里也是一种很好的策略,这要就不用每次用常量时都要带个常量类名。
另外有两个类,都在相同的包com.opensymphony.webwork.dispatcher下,一是CoolUriServletDispatcher,二是FilterDispatcher。前面那个继承自ServletDispatcher,后面的则不同,完全是一个filter。搜了一下网络,好象没人讲明白是它究竟干什么,其api文档也没有说明这个类,暂且放下。
Cool类不讲它。讲ServletDispatcher。
When a request enters the servlet the following things will happen:
a.The action name is parsed from the servlet path (i.e., /foo/bar/MyAction.action -> MyAction).
b.A context consisting of the request, response, parameters, session and application properties is created.
c.An XWork ActionProxy object is instantiated (wraps an Action) using the action name, path, and context then executed.
d.Action output will channel back through the response to the user.
它比我讲的清楚。
1.init方法
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
LocalizedTextUtil.addDefaultResourceBundle("com/opensymphony/webwork/webwork-messages");
//check for configuration reloading
if ("true".equalsIgnoreCase(Configuration.getString("webwork.configuration.xml.reload"))) {
FileManager.setReloadingConfigs(true);
}
if (Configuration.isSet("webwork.i18n.encoding")) {
encoding = Configuration.getString("webwork.i18n.encoding");
}
if (Configuration.isSet("webwork.locale")) {
locale = localeFromString(Configuration.getString("webwork.locale"));
}
// store a reference to ourself into the SessionContext so that we can generate a PageContext
servletConfig.getServletContext().setAttribute("webwork.servlet", this);
// test wether param-access workaround needs to be enabled
if (servletConfig.getServletContext().getServerInfo().indexOf("WebLogic") >= 0) {
log.info("WebLogic server detected. Enabling parameter access work-around.");
paramsWorkaroundEnabled = true;
} else {
log.debug("Parameter access work-around disabled.");
}
}
很好理解,我们应该看看default.properties和webwork.properties。
注意还有一个com/opensymphony/webwork/webwork-messages.properties
2.service()方法
request = wrapRequest(request);
serviceAction(request, response, getNameSpace(request), getActionName(request), getRequestMap(request), getParameterMap(request), getSessionMap(request), getApplicationMap());
//如果是上传文件,则将request改为MultiPartRequestWrapper。有两个参数savedir与maxsize
//解析四个对象,分别进入四个实现了Map接口的对象。三大对象,RequestMap,SessionMap,ApplicationMap。
3.serviceAction()方法
HashMap extraContext = createContextMap(requestMap, parameterMap, sessionMap, applicationMap, request, response, getServletConfig());
extraContext.put(SERVLET_DISPATCHER, this);
// If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
OgnlValueStack stack = (OgnlValueStack) request.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY);
if (stack != null) {
extraContext.put(ActionContext.VALUE_STACK,new OgnlValueStack(stack));
}
//我不太喜欢的OgnlValueStack,同一个请求会被重新创建。
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext);
request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack());//ignore it....
proxy.execute();
//创建DefaultActionProxyFactory,然后创建ActionProxy
protected void prepare() throws Exception {
invocation = ActionProxyFactory.getFactory().createActionInvocation(this, extraContext);
}
//调用DefaultActionProxyFactory创建DefaultActionInvocation。并
//注入自己及相关对象,然后产生所需对象。
protected Action action;
protected ActionProxy proxy;
protected List preResultListeners;
protected Map extraContext;
protected ActionContext invocationContext;
protected Iterator interceptors;
protected OgnlValueStack stack;
protected Result result;
protected String resultCode;
protected boolean executed = false;
protected boolean pushAction = true;
//Action ActionContext OgnlValueStack三个对象重要。
protected DefaultActionInvocation(ActionProxy proxy, Map extraContext, boolean pushAction) throws Exception {
this.proxy = proxy;
this.extraContext = extraContext;
this.pushAction = pushAction;
init();
}
//注意init()方法。
private void init() throws Exception {
Map contextMap = createContextMap();
createAction();
if (pushAction) {
stack.push(action);
}
invocationContext = new ActionContext(contextMap);
invocationContext.setName(proxy.getActionName());
// get a new List so we don't get problems with the iterator if someone changes the list
List interceptorList = new ArrayList(proxy.getConfig().getInterceptors());
interceptors = interceptorList.iterator();
}
//得到运行所需的所有参数
至此,ServletDispacher的serviceAction中的这一句
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext);
//完成第一句,创建过程。
request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack());
proxy.execute();
// If there was a previous value stack then set it back onto the request
if (stack != null){
request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY,stack);
}
//这一句可以说明,action是每次实例化的。
action = ObjectFactory.getObjectFactory().buildAction(proxy.getConfig());
//看的很仓促,欢迎指点。明天写一下执行过程。接下来是转向过程。然后是一些边边角角吧。
分享到:
相关推荐
WebWork-2.1.7 中文开发文档-小白
webwork2.1.7
webwork框架2.1.7版本
webwork 2.1.7 版 原版留存 xwork.jar
webwork2.1.7+spring+hibernate+oracle整合
描述: webwork-2.1.7.jar 修改: package com.opensymphony.webwork.interceptor.FileUploadInterceptor 类line88 to line 93 修复bug:upload empty file cause nullponit exception 博文链接:...
webwork in action_第1部分-WebWork简介_第2章-WebWork方式的Hello World
webwork in action 书籍 源代码
webwork2.1.7+spring+hibernate3+oracle+weblogic8.1登录示例(详解): 通过输入用户名查询出数据库中的对应数据
xwork源代码(webwork源代码,xwork source,)xwork源代码(webwork源代码,xwork source,)
webwork2中文参考1.0a-pdf
详细介绍了,webwork2的配置及action映射,验证框架等,里面有jar包,源代码
webwork-2.1.jar webwork相关jar包
剖析WEBWORK源代码,是个大师的作品,要想深入学习webwork的朋友又必要学习以下,帮助很大。
《webwork in action》源代码.rar
webwork-2.2.2和xwork1.1.3的jar包and源代码
关键是正对最新版webwork,在action中进行操作,说得很细致,有源代码实例。也将异常情况处理列出来了。
NULL 博文链接:https://mysun.iteye.com/blog/378564
作者:钱安川 前言 本篇文章并没有太多WebWork 的实战代码细节。本人非常希望能充当一名导游的角色,带领读者逐步游览webwork深入浅出-----WebWork的功能特性和原理。在第一章,我们将提出基于三层架构的Web层需要...