亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

struts2核心工作流程與原理

系統 1929 0

這是Struts2官方站點提供的Struts 2 的整體結構。
struts2核心工作流程與原理
  一個請求在Struts2框架中的處理大概分為以下幾個步驟:

  1. 客戶端提起一個(HttpServletRequest)請求,如上文在瀏覽器中輸入”http://localhost:8080/TestMvc/add.action”就是提起一個(HttpServletRequest)請求。
  2. 請求被提交到一系列(主要是三層)的過濾器(Filter),如(ActionContextCleanUp、其他過濾器(SiteMesh等)、 FilterDispatcher)。注意這里是有順序的,先ActionContextCleanUp,再其他過濾器(SiteMesh等)、最后到FilterDispatcher。
  3. FilterDispatcher是控制器的核心,就是mvc中c控制層的核心。下面粗略的分析下我理解的FilterDispatcher工作流程和原理:FilterDispatcher進行初始化并啟用核心doFilter

    其代碼如下:

    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); //正如這個方法名字一樣進行locale、encoding以及特殊request?parameters設置
    8. ???????? try ?...{
    9. ????????????request?=?du.wrapRequest(request,?servletContext); //對request進行包裝
    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. //整個框架最最核心的方法,下面分析
    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. //這個方法詢問ActionMapper是否需要調用某個Action來處理這個(request)請求,如果ActionMapper決定需要調用某個Action,FilterDispatcher把請求的處理交給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());?? //實例化Map請求?,詢問ActionMapper是否需要調用某個Action來處理這個(request)請求
    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把請求的處理交給ActionProxy,下面是ServletDispatcher的?TODO:?
    57. ????????????request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY,?proxy.getInvocation().getStack());?
    58. ????????????proxy.execute();?
    59. ????????? //通過代理模式執行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. }?
    FilterDispatcher詢問ActionMapper是否需要調用某個Action來處理這個(request)請求,如果ActionMapper決定需要調用某個Action,FilterDispatcher把請求的處理交給ActionProxy。
  4. ActionProxy通過Configuration Manager(struts.xml)詢問框架的配置文件,找到需要調用的Action類.
    如上文的struts.xml配置
    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 >
    如果提交請求的是add.action,那么找到的Action類就是edisundong.AddAction。
  5. ActionProxy創建一個ActionInvocation的實例,同時ActionInvocation通過代理模式調用Action。但在調用之前ActionInvocation會根據配置加載Action相關的所有Interceptor。(Interceptor是struts2另一個核心級的概念)

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

    ActionInvocation 是Xworks 中Action 調度的核心。而對Interceptor 的調度,也正是由ActionInvocation負責。ActionInvocation 是一個接口, 而DefaultActionInvocation 則是Webwork 對ActionInvocation的默認實現。

    Interceptor 的調度流程大致如下:
    1. ActionInvocation初始化時,根據配置,加載Action相關的所有Interceptor。
    2. 通過ActionInvocation.invoke方法調用Action實現時,執行Interceptor。

    Interceptor將很多功能從我們的Action中獨立出來,大量減少了我們Action的代碼,獨立出來的行為具有很好的重用性。XWork、WebWork的許多功能都是有Interceptor實現,可以在配置文件中組裝Action用到的Interceptor,它會按照你指定的順序,在Action執行前后運行。
    那么什么是攔截器。
    攔截器就是AOP(Aspect-Oriented Programming)的一種實現。(AOP是指用于在某個方法或字段被訪問之前,進行攔截然后在之前或之后加入某些操作。)
    攔截器的例子這里就不展開了。
    struts-default.xml文件摘取的內容:
    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" ? /> ?
    一旦Action執行完畢,ActionInvocation負責根據struts.xml中的配置找到對應的返回結果。如上文中將結構返回“add.jsp”,但大部分時候都是返回另外一個action,那么流程又得走一遍………

總結:

  Struts2的工作流就只有這7步,比起Struts1簡單了很多(本人能力有限,struts2更多的東西現在還看不明白)。網上有很多很多的關于.net和java的比較之類的文章,可是有幾個作者是真正用過java和.net的呢?更多的評論都是人云亦云,想當然的評論java和.net。作為技術人千萬不要屁股決定腦袋,關于web的設計模式上.net也不是那么一無是處,java也不是那么完美無缺。下一篇分析下ASP.NET的設計模式(生命周期)。

struts2核心工作流程與原理


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 伊人久久综合影院 | 免费看曰批女人爽的视频网址 | 涩涩精品| www.九九| 久久婷婷激情 | 寡妇一级a毛片免费播放 | 国产精品久久久久久一区二区三区 | 国产精品久久视频 | 久久亚洲精品中文字幕第一区 | 国产午夜精品一二区理论影院 | 狠狠色噜噜狠狠狠狠69 | 久久精品福利 | 99精品视频在线观看免费 | 日日夜夜操视频 | 久久精品无码一区二区日韩av | 99热久久精品免费精品 | 欧美综合亚洲 | 亚洲一区在线免费观看 | 欧美激情在线精品一区二区 | 天天拍天天干 | 久久精品欧美日韩精品 | 在线观看免费亚洲 | 欧美高清不卡 | 免费一看一级毛片全播放 | 久久亚洲免费视频 | 性欧美暴力猛交xxxxx高清 | 日韩欧美国产精品第一页不卡 | 久久亚洲国产视频 | 国产成人亚洲精品91专区高清 | 四虎影院免费看 | 久久经典| 四虎精品永久在线 | 亚洲欧美综合乱码精品成人网 | 国产人成午夜免视频网站 | 中文字幕久精品免费视频 | 一区二区福利视频 | 97国产在线观看 | 久久 精品 | 亚洲一区二区三区久久精品 | 久久91精品国产91久久户 | 亚洲精品在线网 |