程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> WebWork2源碼分析

WebWork2源碼分析

編輯:關於JAVA

Author: zhuam

昨晚一口氣看完了夏昕寫的<>,雖然文檔資料很簡潔,但仍不失為一本好的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);

}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved