Rising Sun

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            148 隨筆 :: 0 文章 :: 22 評(píng)論 :: 0 Trackbacks

          1. Struts2架構(gòu)圖 
          這是Struts2官方站點(diǎn)提供的Struts 2 的整體結(jié)構(gòu)。 


          2. Struts2部分類介紹 

          這部分從Struts2參考文檔中翻譯就可以了。 
          ActionMapper 
                  ActionMapper其實(shí)是HttpServletRequest和Action調(diào)用請(qǐng)求的一個(gè)映射,它屏蔽了Action對(duì)于Request等java Servlet類的依賴。Struts2中它的默認(rèn)實(shí)現(xiàn)類是DefaultActionMapper,ActionMapper很大的用處可以根據(jù)自己的需要來設(shè)計(jì)url格式,它自己也有Restful的實(shí)現(xiàn),具體可以參考文檔的docs\actionmapper.html。 
          ActionProxy&ActionInvocation 
                  Action的一個(gè)代理,由ActionProxyFactory創(chuàng)建,它本身不包括Action實(shí)例,默認(rèn)實(shí)現(xiàn)DefaultActionProxy是由ActionInvocation持有Action實(shí)例。ActionProxy作用是如何取得Action,無論是本地還是遠(yuǎn)程。而ActionInvocation的作用是如何執(zhí)行Action,攔截器的功能就是在ActionInvocation中實(shí)現(xiàn)的。 
          ConfigurationProvider&Configuration 
                  ConfigurationProvider就是Struts2中配置文件的解析器,Struts2中的配置文件主要是尤其實(shí)現(xiàn)類XmlConfigurationProvider及其子類StrutsXmlConfigurationProvider來解析。 

          3. Struts2請(qǐng)求流程 
          1、客戶端發(fā)送請(qǐng)求 
          2、請(qǐng)求先通過ActionContextCleanUp-->FilterDispatcher 
          3、FilterDispatcher通過ActionMapper來決定這個(gè)Request需要調(diào)用哪個(gè)Action 
          4、如果ActionMapper決定調(diào)用某個(gè)Action,F(xiàn)ilterDispatcher把請(qǐng)求的處理交給ActionProxy,這兒已經(jīng)轉(zhuǎn)到它的Delegate--Dispatcher來執(zhí)行 
          5、ActionProxy根據(jù)ActionMapping和ConfigurationManager找到需要調(diào)用的Action類 
          6、ActionProxy創(chuàng)建一個(gè)ActionInvocation的實(shí)例 
          7、ActionInvocation調(diào)用真正的Action,當(dāng)然這涉及到相關(guān)攔截器的調(diào)用 
          8、Action執(zhí)行完畢,ActionInvocation創(chuàng)建Result并返回,當(dāng)然,如果要在返回之前做些什么,可以實(shí)現(xiàn)PreResultListener。添加PreResultListener可以在Interceptor中實(shí)現(xiàn)。 


          另一個(gè)版本:大同小異~ 
          一個(gè)請(qǐng)求在Struts2框架中的處理大概分為以下幾個(gè)步驟: 
          1.客戶端提起一個(gè)(HttpServletRequest)請(qǐng)求,如上文在瀏覽器中輸入”http://localhost:8080/TestMvc/add.action”就是提起一個(gè)(HttpServletRequest)請(qǐng)求。 
          2.請(qǐng)求被提交到一系列(主要是三層)的過濾器(Filter),如(ActionContextCleanUp、其他過濾器(SiteMesh等)、 FilterDispatcher)。注意這里是有順序的,先ActionContextCleanUp,再其他過濾器(SiteMesh等)、最后到FilterDispatcher。 
          3.FilterDispatcher是控制器的核心,就是mvc中c控制層的核心。下面粗略的分析下我理解的FilterDispatcher工作流程和原理:FilterDispatcher進(jìn)行初始化并啟用核心doFilter 

          其代碼如下: 
          Java代碼  收藏代碼
          1. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException ...{   
          2.         HttpServletRequest request = (HttpServletRequest) req;   
          3.         HttpServletResponse response = (HttpServletResponse) res;   
          4.         ServletContext servletContext = filterConfig.getServletContext();   
          5.         // 在這里處理了HttpServletRequest和HttpServletResponse。   
          6.         DispatcherUtils du = DispatcherUtils.getInstance();   
          7.         du.prepare(request, response);//正如這個(gè)方法名字一樣進(jìn)行l(wèi)ocale、encoding以及特殊request parameters設(shè)置   
          8.         try ...{   
          9.             request = du.wrapRequest(request, servletContext);//對(duì)request進(jìn)行包裝   
          10.         } catch (IOException e) ...{   
          11.             String message = "Could not wrap servlet request with MultipartRequestWrapper!";   
          12.             LOG.error(message, e);   
          13.             throw new ServletException(message, e);   
          14.         }   
          15.                 ActionMapperIF mapper = ActionMapperFactory.getMapper();//得到action的mapper   
          16.         ActionMapping mapping = mapper.getMapping(request);// 得到action 的 mapping   
          17.         if (mapping == null) ...{   
          18.             // there is no action in this request, should we look for a static resource?   
          19.             String resourcePath = RequestUtils.getServletPath(request);   
          20.             if ("".equals(resourcePath) && null != request.getPathInfo()) ...{   
          21.                 resourcePath = request.getPathInfo();   
          22.             }   
          23.             if ("true".equals(Configuration.get(WebWorkConstants.WEBWORK_SERVE_STATIC_CONTENT))    
          24.                     && resourcePath.startsWith("/webwork")) ...{   
          25.                 String name = resourcePath.substring("/webwork".length());   
          26.                 findStaticResource(name, response);   
          27.             } else ...{   
          28.                 // this is a normal request, let it pass through   
          29.                 chain.doFilter(request, response);   
          30.             }   
          31.             // WW did its job here   
          32.             return;   
          33.         }   
          34.         Object o = null;   
          35.         try ...{   
          36.             //setupContainer(request);   
          37.             o = beforeActionInvocation(request, servletContext);   
          38.            //整個(gè)框架最最核心的方法,下面分析   
          39.             du.serviceAction(request, response, servletContext, mapping);   
          40.         } finally ...{   
          41.             afterActionInvocation(request, servletContext, o);   
          42.             ActionContext.setContext(null);   
          43.         }   
          44.     }   
          45. du.serviceAction(request, response, servletContext, mapping);   
          46. //這個(gè)方法詢問ActionMapper是否需要調(diào)用某個(gè)Action來處理這個(gè)(request)請(qǐng)求,如果ActionMapper決定需要調(diào)用某個(gè)Action,F(xiàn)ilterDispatcher把請(qǐng)求的處理交給ActionProxy   
          47. public void serviceAction(HttpServletRequest request, HttpServletResponse response, String namespace, String actionName, Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap) ...{    
          48.         HashMap extraContext = createContextMap(requestMap, parameterMap, sessionMap, applicationMap, request, response, getServletConfig());  //實(shí)例化Map請(qǐng)求 ,詢問ActionMapper是否需要調(diào)用某個(gè)Action來處理這個(gè)(request)請(qǐng)求   
          49.         extraContext.put(SERVLET_DISPATCHER, this);    
          50.         OgnlValueStack stack = (OgnlValueStack) request.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY);    
          51.         if (stack != null) ...{    
          52.             extraContext.put(ActionContext.VALUE_STACK,new OgnlValueStack(stack));    
          53.         }    
          54.         try ...{    
          55.             ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext);    
          56. //這里actionName是通過兩道getActionName解析出來的, FilterDispatcher把請(qǐng)求的處理交給ActionProxy,下面是ServletDispatcher的 TODO:    
          57.             request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack());    
          58.             proxy.execute();    
          59.            //通過代理模式執(zhí)行ActionProxy   
          60.             if (stack != null)...{    
          61.                 request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY,stack);    
          62.             }    
          63.         } catch (ConfigurationException e) ...{    
          64.             log.error("Could not find action", e);    
          65.             sendError(request, response, HttpServletResponse.SC_NOT_FOUND, e);    
          66.         } catch (Exception e) ...{    
          67.             log.error("Could not execute action", e);    
          68.             sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);    
          69.         }    
          70. }    



          4.FilterDispatcher詢問ActionMapper是否需要調(diào)用某個(gè)Action來處理這個(gè)(request)請(qǐng)求,如果ActionMapper決定需要調(diào)用某個(gè)Action,F(xiàn)ilterDispatcher把請(qǐng)求的處理交給ActionProxy。 
          5.ActionProxy通過Configuration Manager(struts.xml)詢問框架的配置文件,找到需要調(diào)用的Action類. 
          如上文的struts.xml配置 

          Java代碼  收藏代碼
          1. <?xml version="1.0" encoding="GBK"?>   
          2.  <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">   
          3.  <struts>   
          4.      <include file="struts-default.xml"/>   
          5.      <package name="struts2" extends="struts-default">   
          6.          <action name="add"    
          7.              class="edisundong.AddAction" >   
          8.              <result>add.jsp</result>   
          9.          </action>       
          10.      </package>   
          11.  </struts>   

          如果提交請(qǐng)求的是add.action,那么找到的Action類就是edisundong.AddAction。 
          6.ActionProxy創(chuàng)建一個(gè)ActionInvocation的實(shí)例,同時(shí)ActionInvocation通過代理模式調(diào)用Action。但在調(diào)用之前ActionInvocation會(huì)根據(jù)配置加載Action相關(guān)的所有Interceptor。(Interceptor是struts2另一個(gè)核心級(jí)的概念) 

          下面我們來看看ActionInvocation是如何工作的: 

          ActionInvocation 是Xworks 中Action 調(diào)度的核心。而對(duì)Interceptor 的調(diào)度,也正是由ActionInvocation負(fù)責(zé)。ActionInvocation 是一個(gè)接口, 而DefaultActionInvocation 則是Webwork 對(duì)ActionInvocation的默認(rèn)實(shí)現(xiàn)。 

          Interceptor 的調(diào)度流程大致如下: 
          1. ActionInvocation初始化時(shí),根據(jù)配置,加載Action相關(guān)的所有Interceptor。 
          2. 通過ActionInvocation.invoke方法調(diào)用Action實(shí)現(xiàn)時(shí),執(zhí)行Interceptor。 

          Interceptor將很多功能從我們的Action中獨(dú)立出來,大量減少了我們Action的代碼,獨(dú)立出來的行為具有很好的重用性。XWork、WebWork的許多功能都是有Interceptor實(shí)現(xiàn),可以在配置文件中組裝Action用到的Interceptor,它會(huì)按照你指定的順序,在Action執(zhí)行前后運(yùn)行。 
          那么什么是攔截器。 
          攔截器就是AOP(Aspect-Oriented Programming)的一種實(shí)現(xiàn)。(AOP是指用于在某個(gè)方法或字段被訪問之前,進(jìn)行攔截然后在之前或之后加入某些操作。) 
          攔截器的例子這里就不展開了。 
          struts-default.xml文件摘取的內(nèi)容: 
          Java代碼  收藏代碼
          1. < interceptor name ="alias" class ="com.opensymphony.xwork2.interceptor.AliasInterceptor" />    
          2. < interceptor name ="autowiring" class ="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor" />    
          3. < interceptor name ="chain" class ="com.opensymphony.xwork2.interceptor.ChainingInterceptor" />    
          4. < interceptor name ="conversionError" class ="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor" />    
          5. < interceptor name ="createSession" class ="org.apache.struts2.interceptor.CreateSessionInterceptor" />    
          6. < interceptor name ="debugging" class ="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />    
          7. < interceptor name ="external-ref" class ="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor" />    
          8. < interceptor name ="execAndWait" class ="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor" />    
          9. < interceptor name ="exception" class ="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor" />    
          10. < interceptor name ="fileUpload" class ="org.apache.struts2.interceptor.FileUploadInterceptor" />    
          11. < interceptor name ="i18n" class ="com.opensymphony.xwork2.interceptor.I18nInterceptor" />    
          12. < interceptor name ="logger" class ="com.opensymphony.xwork2.interceptor.LoggingInterceptor" />    
          13. < interceptor name ="model-driven" class ="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor" />    
          14. < interceptor name ="scoped-model-driven" class ="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor" />    
          15. < interceptor name ="params" class ="com.opensymphony.xwork2.interceptor.ParametersInterceptor" />    
          16. < interceptor name ="prepare" class ="com.opensymphony.xwork2.interceptor.PrepareInterceptor" />    
          17. < interceptor name ="static-params" class ="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor" />    
          18. < interceptor name ="scope" class ="org.apache.struts2.interceptor.ScopeInterceptor" />    
          19. < interceptor name ="servlet-config" class ="org.apache.struts2.interceptor.ServletConfigInterceptor" />    
          20. < interceptor name ="sessionAutowiring" class ="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor" />    
          21. < interceptor name ="timer" class ="com.opensymphony.xwork2.interceptor.TimerInterceptor" />    
          22. < interceptor name ="token" class ="org.apache.struts2.interceptor.TokenInterceptor" />    
          23. < interceptor name ="token-session" class ="org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />    
          24. < interceptor name ="validation" class ="com.opensymphony.xwork2.validator.ValidationInterceptor" />    
          25. < interceptor name ="workflow" class ="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor" />    
          26. < interceptor name ="store" class ="org.apache.struts2.interceptor.MessageStoreInterceptor" />    
          27. < interceptor name ="checkbox" class ="org.apache.struts2.interceptor.CheckboxInterceptor" />    
          28. < interceptor name ="profiling" class ="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />   



          7.一旦Action執(zhí)行完畢,ActionInvocation負(fù)責(zé)根據(jù)struts.xml中的配置找到對(duì)應(yīng)的返回結(jié)果。如上文中將結(jié)構(gòu)返回“add.jsp”,但大部分時(shí)候都是返回另外一個(gè)action,那么流程又得走一遍……… 
          1 客戶端初始化一個(gè)指向Servlet容器(例如Tomcat)的請(qǐng)求 
          2 這個(gè)請(qǐng)求經(jīng)過一系列的過濾器(Filter)(這些過濾器中有一個(gè)叫做ActionContextCleanUp的可選過濾器,這個(gè)過濾器對(duì)于Struts2和其他框架的集成很有幫助,例如:SiteMesh Plugin) 
          3 接著FilterDispatcher被調(diào)用,F(xiàn)ilterDispatcher詢問ActionMapper來決定這個(gè)請(qǐng)是否需要調(diào)用某個(gè)Action 
          4 如果ActionMapper決定需要調(diào)用某個(gè)Action,F(xiàn)ilterDispatcher把請(qǐng)求的處理交給ActionProxy 
          5 ActionProxy通過Configuration Manager詢問框架的配置文件,找到需要調(diào)用的Action類 
          6 ActionProxy創(chuàng)建一個(gè)ActionInvocation的實(shí)例。 
          7 ActionInvocation實(shí)例使用命名模式來調(diào)用,在調(diào)用Action的過程前后,涉及到相關(guān)攔截器(Intercepter)的調(diào)用。 
          8 一旦Action執(zhí)行完畢,ActionInvocation負(fù)責(zé)根據(jù)struts.xml中的配置找到對(duì)應(yīng)的返回結(jié)果。返回結(jié)果通常是(但不總是,也可 能是另外的一個(gè)Action鏈)一個(gè)需要被表示的JSP或者FreeMarker的模版。在表示的過程中可以使用Struts2 框架中繼承的標(biāo)簽。在這個(gè)過程中需要涉及到ActionMapper 

          在上述過程中所有的對(duì)象(Action,Results,Interceptors,等)都是通過ObjectFactory來創(chuàng)建的。


          posted on 2013-03-05 09:31 brock 閱讀(215) 評(píng)論(0)  編輯  收藏 所屬分類: hibernate+struts+spring

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 会昌县| 罗山县| 门头沟区| 黔西县| 商水县| 会宁县| 新疆| 马关县| 临城县| 开江县| 开远市| 板桥市| 高淳县| 九龙坡区| 汉阴县| 静安区| 张家港市| 鄂伦春自治旗| 彭泽县| 海门市| 安国市| 曲松县| 百色市| 万年县| 康保县| 呈贡县| 尼玛县| 江西省| 军事| 阳原县| 武城县| 长治市| 井研县| 翁牛特旗| 咸阳市| 曲周县| 南康市| 鹤壁市| 金沙县| 荔波县| 崇左市|