`
苏飞
  • 浏览: 69854 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

webwork2.1.7源代码分析之--初始化及创建action过程(草稿)

阅读更多

  写文章是一件困难的事,如果要讲的简单明了更难。

  一,总体说明
  来看看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());



   //看的很仓促,欢迎指点。明天写一下执行过程。接下来是转向过程。然后是一些边边角角吧。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics