天天看點

WebWork2.2工作原理總結

WebWork2.2 工作原理總結

一、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 架構的初始化。

二、WebWork的使用者請求處理過程 所有以web.xml中映射FilterDispatcher結尾的服務請求将由FilterDispatcher進行處理。

1、從使用者請求的服務名中解析出對應Action的名稱。    具體完成是:戶按webwork規則請求時,伺服器會調用FilterDispatcher的 doFilter 方法,完成第二步的内容。

         2、周遊 HttpServletRequest、HttpSession、ServletContext 中的資料,并将其複制到Webwork的Map中,為下一步建立Action事例打下基礎。

具體完成是:過調用DispatcherUtils的 serviceAction 方法中的

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="[email protected]@[email protected]@[email protected]@[email protected]@5xe" filled="f" stroked="f" id="_x0000_t75"> 

WebWork2.2工作原理總結

</v:shapetype>

繼續閱讀