溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

WebWork的框架初始化過程和用戶請求處理過程

發(fā)布時間:2021-09-14 15:04:58 來源:億速云 閱讀:136 作者:chen 欄目:編程語言

本篇內(nèi)容主要講解“WebWork的框架初始化過程和用戶請求處理過程”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“WebWork的框架初始化過程和用戶請求處理過程”吧!

一、WebWork的框架初始化過程

WebWork做的項目,在服務(wù)器啟動時完成WebWork的框架初始化。具體是通過Web.xml中配置好的com.opensymphony.xwork.dispatcher.ServletDispatcher(FilterDispatcher)過濾器中的init(ServletConfig servletConfig)方法完成。

并且web.xml中配置好ServletDispatcher的映射,當(dāng)用戶用映射好的結(jié)尾資源請求瀏覽器時,ServletDispatcher會進行請求處理(ServletDispatcher是一個HttpServlet)。

具體實現(xiàn)是通過以下步驟:

1、通過ServletDispatcher中的init方法進行框架的初始化工作:

public void init(ServletConfig servletConfig)        throws ServletException    {        super.init(servletConfig);        DispatcherUtils.initialize(getServletContext());      }

2、init方法又同時調(diào)用DispatcherUtils類的initialize方法創(chuàng)建DispatcherUtils實例,同時間接調(diào)用DispatcherUtils類的init方法初始化Configuration配置,創(chuàng)建對象創(chuàng)建的工廠ObjectFactory和ObjectTypeDeterminer。

至此完成WebWork框架的初始化。

二、WebWork的用戶請求處理過程

所有以web.xml中映射ServletDispatcher結(jié)尾的服務(wù)請求將由ServletDispatcher進行處理。

1、從用戶請求的服務(wù)名中解析出對應(yīng)Action的名稱。

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {    //....      try      {          request = du.wrapRequest(request, getServletContext());      }      catch(IOException e)      {          String message = "Could not wrap servlet request with MultipartRequestWrapper!";          LOG.error(message, e);          throw new ServletException(message, e);      }      du.serviceAction(request, response, getServletContext(), mapping);  }

2、遍歷HttpServletRequest、HttpSession、ServletContext 中的數(shù)據(jù),并將其復(fù)制到Webwork的Map中,為下一步創(chuàng)建Action實例打下基礎(chǔ)。

實現(xiàn):通過過調(diào)用DispatcherUtils的serviceAction方法中的Map extraContext = createContextMap(request, response, mapping, context);完成以上信息的封裝。

3、以上一步封裝好的信息為參數(shù),調(diào)用ActionProxyFactory創(chuàng)建對應(yīng)的ActionProxy實例。ActionProxyFactory 將根據(jù)Xwork 配置文件(xwork.xml)中的設(shè)定,創(chuàng)建ActionProxy實例,ActionProxy中包含了Action的配置信息(包括Action名稱,對應(yīng)實現(xiàn)類等等)。

實現(xiàn):通過ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, name, extraContext, true, false);//創(chuàng)建動態(tài)代理DefaultActionProxyFactory實現(xiàn)ActionProxyFactory的createActionProxy方法,返回new DefaultActionProxy(namespace, actionName, extraContext, true, true);DefaultActionProxy是對ActionProxy的默認實現(xiàn),通過DefaultActionProxy類的DefaultActionProxy(namespace, actionName, extraContext, true, true)構(gòu)造方法實例化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)配置信息的讀取。獲得與此次請求相關(guān)的ActionConfig。

4、ActionProxy創(chuàng)建對應(yīng)的Action實例,并根據(jù)配置進行一系列的處理程序。

通過DefaultActionProxy類的invocation = ActionProxyFactory.getFactory().createActionInvocation(this, extraContext); 

//通過createActionInvocation方法創(chuàng)建動作調(diào)用類ActionInvocation,處理被Action調(diào)用的方法

privatevoid 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";              }          }  }

然后調(diào)用DispatcherUtils的serviceAction方法中的

if (mapping.getResult() != null) {                  Result result = mapping.getResult();                  result.execute(proxy.getInvocation());              } else {                  proxy.execute();  }

完成用戶的最終要執(zhí)行的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調(diào)用提交給ActionInvocation處理。

5、 一旦Action方法返回,ActionInvocation就要查找xwork.xml文件中這個Action的結(jié)果碼(Action Result Code)(一個String如success、input)所對應(yīng)的result,然后執(zhí)行這個result。通常情況下,result會調(diào)用JSP或FreeMarker模板來呈現(xiàn)頁面。當(dāng)呈現(xiàn)頁面時,模板可以使用WebWork提供的一些標(biāo)簽,其中一些組件可以和ActionMapper一起工作來為后面的請求呈現(xiàn)恰當(dāng)?shù)腢RL。

下面我們來看action部分的定義:

<action name="loginAction" class="loginAction">   <result name="success" type="dispatcher">/common/loginedHomeAction!init.action</result>  </action>

這里的result結(jié)點有一個type屬性,這表示此action的結(jié)果應(yīng)該怎樣處理。

再來看看dispatcher類型的result是怎么定義的:

<result-type name="dispatcher" class="com.opensymphony.webwork.dispatcher.ServletDispatcherResult" default="true"/>

到這里就可以知道了處理是交給ServletDispatcherResult類來做的。

ServletDispatcherResult類繼承了WebWorkResultSupport類,而WebWorkResultSupport實現(xiàn)了com.opensymphony.xwork.Result接口,此接口用來處理action的結(jié)果。WebWorkResultSupport類定義了一個抽象的方法——doExecute,此方法用于實現(xiàn)對Result的處理。

下面來看看ServletDispatcherResult是怎么處理的:

public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {           PageContext pageContext = ServletActionContext.getPageContext();           if (pageContext != null) {              pageContext.include(finalLocation);          } else {              HttpServletRequest request = ServletActionContext.getRequest();              HttpServletResponse response = ServletActionContext.getResponse();              RequestDispatcher dispatcher = request.getRequestDispatcher(finalLocation);               // if the view doesn't exist, let's do a 404              if (dispatcher == null) {                  response.sendError(404, "result '" + finalLocation + "' not found");                   return;              }               // If we're included, then include the view              // Otherwise do forward               // This allow the page to, for example, set content type               if (!response.isCommitted() && (request.getAttribute("javax.servlet.include.servlet_path") == null)) {                  request.setAttribute("webwork.view_uri", finalLocation);                  request.setAttribute("webwork.request_uri", request.getRequestURI());                   dispatcher.forward(request, response);              } else {                  dispatcher.include(request, response);              }          }      }

我們看到,最終調(diào)用的是dispatcher.forward(request, response);這樣就可以成功轉(zhuǎn)到我們的目標(biāo)頁了。

以下代碼為DispatcherUtils中的serviceAction方法中的:

public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping)      throws ServletException  {      Map extraContext = createContextMap(request, response, mapping, context);      OgnlValueStack stack = (OgnlValueStack)request.getAttribute("webwork.valueStack");      if(stack != null)          extraContext.put("com.opensymphony.xwork.util.OgnlValueStack.ValueStack", new OgnlValueStack(stack));      try      {          String namespace = mapping.getNamespace();          String name = mapping.getName();          String method = mapping.getMethod();          String id = request.getParameter("__continue");          if(id != null)          {              Map params = (Map)extraContext.get("com.opensymphony.xwork.ActionContext.parameters");              params.remove("__continue");              extraContext.put("__continue", id);          }          ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, name, extraContext, true, false);          proxy.setMethod(method);          request.setAttribute("webwork.valueStack", proxy.getInvocation().getStack());          if(mapping.getResult() != null)          {              Result result = mapping.getResult();              result.execute(proxy.getInvocation());          } else          {              proxy.execute();          }          if(stack != null)              request.setAttribute("webwork.valueStack", stack);      }      catch(ConfigurationException e)      {          LOG.error("Could not find action", e);          sendError(request, response, 404, e);      }      catch(Exception e)      {          String msg = "Could not execute action";          LOG.error(msg, e);          throw new ServletException(msg, e);      }  }

三、WebWork的執(zhí)行流程圖

WebWork的框架初始化過程和用戶請求處理過程

到此,相信大家對“WebWork的框架初始化過程和用戶請求處理過程”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI