偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

WebWork的執(zhí)行流程

開(kāi)發(fā) 后端
本文介紹WebWork的工作流程。

 一、WebWork的框架初始化過(guò)程

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

并且web.xml中配置好ServletDispatcher的映射,當(dāng)用戶(hù)用映射好的結(jié)尾資源請(qǐng)求瀏覽器時(shí),ServletDispatcher會(huì)進(jìn)行請(qǐng)求處理(ServletDispatcher是一個(gè)HttpServlet)。

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

1、通過(guò)ServletDispatcher中的init方法進(jìn)行框架的初始化工作:

  1. public void init(ServletConfig servletConfig)  
  2.       throws ServletException  
  3.   {  
  4.       super.init(servletConfig);  
  5.       DispatcherUtils.initialize(getServletContext());  
  6.  
  7.    } 

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

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

二、WebWork的用戶(hù)請(qǐng)求處理過(guò)程

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

1、從用戶(hù)請(qǐng)求的服務(wù)名中解析出對(duì)應(yīng)Action的名稱(chēng)。

  1. public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {  
  2.   //....  
  3.     try  
  4.     {  
  5.         request = du.wrapRequest(request, getServletContext());  
  6.     }  
  7.     catch(IOException e)  
  8.     {  
  9.         String message = "Could not wrap servlet request with MultipartRequestWrapper!";  
  10.         LOG.error(message, e);  
  11.         throw new ServletException(message, e);  
  12.     }  
  13.     du.serviceAction(request, response, getServletContext(), mapping);  

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

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

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

實(shí)現(xiàn):通過(guò)ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, name, extraContext, true, false);//創(chuàng)建動(dòng)態(tài)代理DefaultActionProxyFactory實(shí)現(xiàn)ActionProxyFactory的createActionProxy方法,返回new DefaultActionProxy(namespace, actionName, extraContext, true, true);DefaultActionProxy是對(duì)ActionProxy的默認(rèn)實(shí)現(xiàn),通過(guò)DefaultActionProxy類(lèi)的DefaultActionProxy(namespace, actionName, extraContext, true, true)構(gòu)造方法實(shí)例化DefaultActionProxy,同時(shí)得到用戶(hù)請(qǐng)求的actionName及namespace,并通過(guò)config = ConfigurationManager.getConfiguration().getRuntimeConfiguration().getActionConfig(namespace, actionName);
ConfigurationManager的

  1. public static synchronized Configuration getConfiguration()  
  2. {  
  3.     if(configurationInstance == null)  
  4.     {  
  5.         configurationInstance = new DefaultConfiguration();  
  6.         try  
  7.         {  
  8.             configurationInstance.reload();  
  9.         }  
  10.         catch(ConfigurationException e)  
  11.         {  
  12.             configurationInstance = null;  
  13.             throw e;  
  14.         }  
  15.     } else  
  16.     {  
  17.         conditionalReload();  
  18.     }  
  19.     return configurationInstance;  

完成對(duì)xwork.xml(具體操作類(lèi)是XmlConfigurationProvider)配置信息的讀取。獲得與此次請(qǐng)求相關(guān)的ActionConfig。

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

通過(guò)DefaultActionProxy類(lèi)的invocation = ActionProxyFactory.getFactory().createActionInvocation(this, extraContext);  

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

  1. privatevoid resolveMethod() {  
  2.         // if the method is set to null, use the one from the configuration  
  3.         // if the one from the configuration is also null, use "execute"  
  4.         if (!TextUtils.stringSet(this.method)) {  
  5.             this.method = config.getMethodName();  
  6.             if (!TextUtils.stringSet(this.method)) {  
  7.                 this.method = "execute";  
  8.             }  
  9.         }  

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

  1. if (mapping.getResult() != null) {  
  2.                 Result result = mapping.getResult();  
  3.                 result.execute(proxy.getInvocation());  
  4.             } else {  
  5.                 proxy.execute();  

完成用戶(hù)的最終要執(zhí)行的action方法。

  1. public String execute() throws Exception {  
  2.         ActionContext nestedContext = ActionContext.getContext();  
  3.         ActionContext.setContext(invocation.getInvocationContext());  
  4.    
  5.         String retCode = null;  
  6.    
  7.         try {  
  8.             retCode = invocation.invoke();  
  9.         } finally {  
  10.             if (cleanupContext) {  
  11.                 ActionContext.setContext(nestedContext);  
  12.             }  
  13.         }  
  14.    
  15.         return retCode;  
  16.     } 

最終處理ActionContext對(duì)象,將Action調(diào)用提交給ActionInvocation處理。

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

下面我們來(lái)看action部分的定義:

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

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

再來(lái)看看dispatcher類(lèi)型的result是怎么定義的:

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

到這里就可以知道了處理是交給ServletDispatcherResult類(lèi)來(lái)做的。

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

下面來(lái)看看ServletDispatcherResult是怎么處理的:

  1. public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {  
  2.  
  3.         PageContext pageContext = ServletActionContext.getPageContext();  
  4.  
  5.         if (pageContext != null) {  
  6.             pageContext.include(finalLocation);  
  7.         } else {  
  8.             HttpServletRequest request = ServletActionContext.getRequest();  
  9.             HttpServletResponse response = ServletActionContext.getResponse();  
  10.             RequestDispatcher dispatcher = request.getRequestDispatcher(finalLocation);  
  11.  
  12.             // if the view doesn't exist, let's do a 404  
  13.             if (dispatcher == null) {  
  14.                 response.sendError(404, "result '" + finalLocation + "' not found");  
  15.  
  16.                 return;  
  17.             }  
  18.  
  19.             // If we're included, then include the view  
  20.             // Otherwise do forward   
  21.             // This allow the page to, for example, set content type   
  22.             if (!response.isCommitted() && (request.getAttribute("javax.servlet.include.servlet_path") == null)) {  
  23.                 request.setAttribute("webwork.view_uri", finalLocation);  
  24.                 request.setAttribute("webwork.request_uri", request.getRequestURI());  
  25.  
  26.                 dispatcher.forward(request, response);  
  27.             } else {  
  28.                 dispatcher.include(request, response);  
  29.             }  
  30.         }  
  31.     } 

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

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

  1. public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping)  
  2.     throws ServletException  
  3. {  
  4.     Map extraContext = createContextMap(request, response, mapping, context);  
  5.     OgnlValueStack stack = (OgnlValueStack)request.getAttribute("webwork.valueStack");  
  6.     if(stack != null)  
  7.         extraContext.put("com.opensymphony.xwork.util.OgnlValueStack.ValueStack", new OgnlValueStack(stack));  
  8.     try  
  9.     {  
  10.         String namespace = mapping.getNamespace();  
  11.         String name = mapping.getName();  
  12.         String method = mapping.getMethod();  
  13.         String id = request.getParameter("__continue");  
  14.         if(id != null)  
  15.         {  
  16.             Map params = (Map)extraContext.get("com.opensymphony.xwork.ActionContext.parameters");  
  17.             params.remove("__continue");  
  18.             extraContext.put("__continue", id);  
  19.         }  
  20.         ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, name, extraContext, true, false);  
  21.         proxy.setMethod(method);  
  22.         request.setAttribute("webwork.valueStack", proxy.getInvocation().getStack());  
  23.         if(mapping.getResult() != null)  
  24.         {  
  25.             Result result = mapping.getResult();  
  26.             result.execute(proxy.getInvocation());  
  27.         } else  
  28.         {  
  29.             proxy.execute();  
  30.         }  
  31.         if(stack != null)  
  32.             request.setAttribute("webwork.valueStack", stack);  
  33.     }  
  34.     catch(ConfigurationException e)  
  35.     {  
  36.         LOG.error("Could not find action", e);  
  37.         sendError(request, response, 404, e);  
  38.     }  
  39.     catch(Exception e)  
  40.     {  
  41.         String msg = "Could not execute action";  
  42.         LOG.error(msg, e);  
  43.         throw new ServletException(msg, e);  
  44.     }  

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

WebWork的執(zhí)行流程圖

【編輯推薦】

  1. WebWork如何實(shí)現(xiàn)文件上傳配置過(guò)程
  2. WebWork下載的實(shí)現(xiàn)
  3. 通過(guò)WebWork實(shí)現(xiàn)HelloWorld
  4. Tapestry 5組件事件詳解
  5. Tapestry5的性能改進(jìn)淺析

 

責(zé)任編輯:雪峰 來(lái)源: CSDN博客
相關(guān)推薦

2009-07-08 09:55:51

WebWork下載

2009-07-14 15:52:00

WebWork文件下載

2009-07-14 16:08:41

WebWork學(xué)習(xí)

2009-07-14 17:34:53

Webwork配置

2009-07-14 01:00:43

WebWorkActionConte

2009-07-16 14:08:14

webwork配置

2009-07-08 10:56:04

WebWork

2009-07-10 11:02:17

WebWork參數(shù)配置

2009-07-16 16:51:56

WebWork驗(yàn)證機(jī)制

2009-07-16 16:08:30

WebWork Act

2009-07-16 16:01:54

WebWork敏捷開(kāi)發(fā)

2009-07-08 10:11:30

WebWork

2009-07-14 14:04:36

WebWork框架

2009-07-09 18:24:00

WebWork與Spr

2009-07-14 17:10:44

struts2webwork

2009-07-16 17:42:47

WebWork配置

2009-07-09 15:55:18

WebWork配置文件

2009-07-14 17:53:11

WebWork安裝WebWork配置

2009-07-16 14:58:03

WebWork安裝WebWork配置

2009-07-16 16:27:26

Struts WebW
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)