Author: zhuam
昨晚一口气看完了夏昕写的<<Webwork2_Guide>>,虽然文档资料很简洁,但仍不失为一本好的WebWork2书籍,看的出作者的经验和能力都是非常的老道,在此向作者的开源精神致敬,并在此引用夏昕的那句话So many open source projects, Why not Open your Documents?
今天下载了最新的WebWork2版本, 开始了源码分析,这份文档只能算是我的个人笔记,也没时间细细校对,且个人能力有限,难免有许多分析错误或笔误的地方,还望各位指正.
WebWork1.x版本是采用紧偶合的架构,类似如Struts ,而WebWork2.x版本后采用了新的架构 WebWork2.x+XWork1.x 就是因为他采用了这样架构所以才吸引我去看他源码的啊!新的架构分离了与Servlet API的紧偶合,这样我们的系统结构将更加的清晰,且系统测试与系统移植将来的更加方便,其实WebWork1.x版本我也没有看过,所谓的紧偶合也是看网上是这么说的,没有发言权啊,呵呵!
在新的架构中WebWork2.x的作用是负责将用户的HTTP请求分离出来,使的请求完全的脱离Servlet API,然后将这些请求用Map的方式传入XWork1.x,且XWork1.x通过Interceptor 将Map 中的数据传到我们的VO中,然后由对应的自定义Action来调用.
下面首先来看WebWork2.x怎么分离HTTP请求,其实他是通过ServletDispatcher来达到目的.代码如下所示:
public static HashMap createContextMap(Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap, HttpServletRequest request, HttpServletResponse response, ServletConfig servletConfig) {
HashMap extraContext = new HashMap();
//存放HTTP中上传文件的request session请求
extraContext.put(ActionContext.PARAMETERS, parameterMap);
extraContext.put(ActionContext.SESSION, sessionMap);
extraContext.put(ActionContext.APPLICATION, applicationMap);
extraContext.put(ActionContext.LOCALE, (locale == null) ? request.getLocale() : locale);
extraContext.put(HTTP_REQUEST, request);
extraContext.put(HTTP_RESPONSE, response);
extraContext.put(SERVLET_CONFIG, servletConfig);
extraContext.put(ComponentInterceptor.COMPONENT_MANAGER, request.getAttribute(ComponentManager.COMPONENT_MANAGER_KEY));
// helpers to get access to request/session/application scope
//这里用于存放HTTP中的request session请求
extraContext.put("request", requestMap);
extraContext.put("session", sessionMap);
extraContext.put("application", applicationMap);
extraContext.put("parameters", parameterMap);
AttributeMap attrMap = new AttributeMap(extraContext);
extraContext.put("attr", attrMap);
return extraContext;
}
以上的是ServletDispatcher 中的方法createContextMap是用于将HTTP请求封装到Map中去,且ServletDispatcher也是WebWork2.x的控制器,负责分离出Action, 他是通过service方法去调用serviceAction方法,然后由serviceAction来完成操作的,代码如下:
public void serviceAction(HttpServletRequest request, HttpServletResponse response, String namespace, String actionName, Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap) {
HashMap extraContext = createContextMap(requestMap, parameterMap, sessionMap, applicationMap, request, response, getServletConfig()); //实例化Map请求
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));
}
try {
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext);
//这里actionName是通过两道getActionName解析出来的,下面是ServletDispatcher的 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)
*/
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);
}
} catch (ConfigurationException e) {
log.error("Could not find action", e);
sendError(request, response, HttpServletResponse.SC_NOT_FOUND, e);
} catch (Exception e) {
log.error("Could not execute action", e);
sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
}
}