麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

struts2核心工作流程和原理

2019-11-11 05:09:04
字體:
供稿:網(wǎng)友
 這是Struts2官方站點提供的Struts 2 的整體結(jié)構(gòu)。  一個請求在Struts2框架中的處理大概分為以下幾個步驟:客戶端提起一個(HttpServletRequest)請求,如上文在瀏覽器中輸入”http://localhost:8080/TestMvc/add.action”就是提起一個(HttpServletRequest)請求。請求被提交到一系列(主要是三層)的過濾器(Filter),如(ActionContextCleanUp、其他過濾器(SiteMesh等)、 FilterDispatcher)。注意這里是有順序的,先ActionContextCleanUp,再其他過濾器(SiteMesh等)、最后到FilterDispatcher。FilterDispatcher是控制器的核心,就是mvc中c控制層的核心。下面粗略的分析下我理解的FilterDispatcher工作流程和原理:FilterDispatcher進行初始化并啟用核心doFilter其代碼如下:public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException ...{        HttpServletRequest request = (HttpServletRequest) req;        HttpServletResponse response = (HttpServletResponse) res;        ServletContext servletContext = filterConfig.getServletContext();        // 在這里處理了HttpServletRequest和HttpServletResponse。        DispatcherUtils du = DispatcherUtils.getInstance();        du.PRepare(request, response);//正如這個方法名字一樣進行l(wèi)ocale、encoding以及特殊request parameters設(shè)置        try ...{            request = du.wrapRequest(request, servletContext);//對request進行包裝        } catch (IOException e) ...{            String message = "Could not wrap servlet request with MultipartRequestWrapper!";            LOG.error(message, e);            throw new ServletException(message, e);        }                ActionMapperIF mapper = ActionMapperFactory.getMapper();//得到action的mapper        ActionMapping mapping = mapper.getMapping(request);// 得到action 的 mapping        if (mapping == null) ...{            // there is no action in this request, should we look for a static resource?            String resourcePath = RequestUtils.getServletPath(request);            if ("".equals(resourcePath) && null != request.getPathInfo()) ...{                resourcePath = request.getPathInfo();            }            if ("true".equals(Configuration.get(WebWorkConstants.WEBWORK_SERVE_STATIC_CONTENT))                     && resourcePath.startsWith("/webwork")) ...{                String name = resourcePath.substring("/webwork".length());                findStaticResource(name, response);            } else ...{                // this is a normal request, let it pass through                chain.doFilter(request, response);            }            // WW did its job here             return;        }        Object o = null;        try ...{            //setupContainer(request);             o = beforeActionInvocation(request, servletContext);//整個框架最最核心的方法,下面分析             du.serviceAction(request, response, servletContext, mapping);        } finally ...{            afterActionInvocation(request, servletContext, o);            ActionContext.setContext(null);        }    }du.serviceAction(request, response, servletContext, mapping);//這個方法詢問ActionMapper是否需要調(diào)用某個Action來處理這個(request)請求,如果ActionMapper決定需要調(diào)用某個Action,F(xiàn)ilterDispatcher把請求的處理交給ActionProxypublic void serviceAction(HttpServletRequest request, HttpServletResponse response, String namespace, String actionName, Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap) ...{         HashMap extraContext = createContextMap(requestMap, parameterMap, sessionMap, applicationMap, request, response, getServletConfig());  //實例化Map請求 ,詢問ActionMapper是否需要調(diào)用某個Action來處理這個(request)請求        extraContext.put(SERVLET_DISPATCHER, this);         OgnlValueStack stack = (OgnlValueStack) request.getAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY);         if (stack != null) ...{             extraContext.put(ActionContext.VALUE_STACK,new OgnlValueStack(stack));         }         try ...{             ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, extraContext); //這里actionName是通過兩道getActionName解析出來的, FilterDispatcher把請求的處理交給ActionProxy,下面是ServletDispatcher的 TODO:             request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, proxy.getInvocation().getStack());             proxy.execute();          //通過代理模式執(zhí)行ActionProxy             if (stack != null)...{                 request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY,stack);             }         } catch (ConfigurationException e) ...{             log.error("Could not find action", e);             sendError(request, response, HttpServletResponse.SC_NOT_FOUND, e);         } catch (Exception e) ...{             log.error("Could not execute action", e);             sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);         } } FilterDispatcher詢問ActionMapper是否需要調(diào)用某個Action來處理這個(request)請求,如果ActionMapper決定需要調(diào)用某個Action,F(xiàn)ilterDispatcher把請求的處理交給ActionProxy。ActionProxy通過Configuration Manager(struts.xml)詢問框架的配置文件,找到需要調(diào)用的Action類.如上文的struts.xml配置<?xml version="1.0" encoding="GBK"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>     <include file="struts-default.xml"/>     <package name="struts2" extends="struts-default">         <action name="add"              class="edisundong.AddAction" >             <result>add.jsp</result>         </action>         </package> </struts>如果提交請求的是add.action,那么找到的Action類就是edisundong.AddAction。 ActionProxy創(chuàng)建一個ActionInvocation的實例,同時ActionInvocation通過代理模式調(diào)用Action。但在調(diào)用之前ActionInvocation會根據(jù)配置加載Action相關(guān)的所有Interceptor。(Interceptor是struts2另一個核心級的概念)下面我們來看看ActionInvocation是如何工作的:ActionInvocation 是Xworks 中Action 調(diào)度的核心。而對Interceptor 的調(diào)度,也正是由ActionInvocation負責。ActionInvocation 是一個接口, 而DefaultActionInvocation 則是Webwork 對ActionInvocation的默認實現(xiàn)。Interceptor 的調(diào)度流程大致如下:1. ActionInvocation初始化時,根據(jù)配置,加載Action相關(guān)的所有Interceptor。2. 通過ActionInvocation.invoke方法調(diào)用Action實現(xiàn)時,執(zhí)行Interceptor。Interceptor將很多功能從我們的Action中獨立出來,大量減少了我們Action的代碼,獨立出來的行為具有很好的重用性。XWork、WebWork的許多功能都是有Interceptor實現(xiàn),可以在配置文件中組裝Action用到的Interceptor,它會按照你指定的順序,在Action執(zhí)行前后運行。那么什么是攔截器。攔截器就是AOP(aspect-Oriented Programming)的一種實現(xiàn)。(AOP是指用于在某個方法或字段被訪問之前,進行攔截然后在之前或之后加入某些操作。)攔截器的例子這里就不展開了。struts-default.xml文件摘取的內(nèi)容:< interceptor name ="alias" class ="com.opensymphony.xwork2.interceptor.AliasInterceptor" /> < interceptor name ="autowiring" class ="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor" /> < interceptor name ="chain" class ="com.opensymphony.xwork2.interceptor.ChainingInterceptor" /> < interceptor name ="conversionError" class ="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor" /> < interceptor name ="createSession" class ="org.apache.struts2.interceptor.CreateSessionInterceptor" /> < interceptor name ="debugging" class ="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" /> < interceptor name ="external-ref" class ="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor" /> < interceptor name ="execAndWait" class ="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor" /> < interceptor name ="exception" class ="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor" /> < interceptor name ="fileUpload" class ="org.apache.struts2.interceptor.FileUploadInterceptor" /> < interceptor name ="i18n" class ="com.opensymphony.xwork2.interceptor.I18nInterceptor" /> < interceptor name ="logger" class ="com.opensymphony.xwork2.interceptor.LoggingInterceptor" /> < interceptor name ="model-driven" class ="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor" /> < interceptor name ="scoped-model-driven" class ="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor" /> < interceptor name ="params" class ="com.opensymphony.xwork2.interceptor.ParametersInterceptor" /> < interceptor name ="prepare" class ="com.opensymphony.xwork2.interceptor.PrepareInterceptor" /> < interceptor name ="static-params" class ="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor" /> < interceptor name ="scope" class ="org.apache.struts2.interceptor.ScopeInterceptor" /> < interceptor name ="servlet-config" class ="org.apache.struts2.interceptor.ServletConfigInterceptor" /> < interceptor name ="sessionAutowiring" class ="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor" /> < interceptor name ="timer" class ="com.opensymphony.xwork2.interceptor.TimerInterceptor" /> < interceptor name ="token" class ="org.apache.struts2.interceptor.TokenInterceptor" /> < interceptor name ="token-session" class ="org.apache.struts2.interceptor.TokenSessionStoreInterceptor" /> < interceptor name ="validation" class ="com.opensymphony.xwork2.validator.ValidationInterceptor" /> < interceptor name ="workflow" class ="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor" /> < interceptor name ="store" class ="org.apache.struts2.interceptor.MessageStoreInterceptor" /> < interceptor name ="checkbox" class ="org.apache.struts2.interceptor.CheckboxInterceptor" /> < interceptor name ="profiling" class ="org.apache.struts2.interceptor.ProfilingActivationInterceptor" /> 一旦Action執(zhí)行完畢,ActionInvocation負責根據(jù)struts.xml中的配置找到對應(yīng)的返回結(jié)果。如上文中將結(jié)構(gòu)返回“add.jsp”,但大部分時候都是返回另外一個action,那么流程又得走一遍………

  總結(jié):

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久国产精品免费视频 | 在线免费观看麻豆 | 19禁国产精品福利视频 | 欧美一级高潮 | 特级黄毛片 | 久久精品一二三区白丝高潮 | 中文字幕在线视频日本 | 欧美成人一级片 | 99精品国产在热久久婷婷 | 在线成人www免费观看视频 | 92看片淫黄大片欧美看国产片 | 黄网站免费在线看 | 色视频在线播放 | 黄色大片在线免费看 | 久久综合一区 | 海外中文字幕在线观看 | 精品国产91一区二区三区 | 国产麻豆交换夫妇 | 欧美性生活网站 | 草草久 | 一级空姐毛片 | 欧美国产91| 污污短视频 | 日韩大片在线永久观看视频网站免费 | 一级做a爱片性色毛片高清 日本一区二区在线看 | 国产免费一级淫片a级中文 99国产精品自拍 | 一级毛片在线观看免费 | 成年人在线免费播放视频 | av免费av | 精品国产91久久久久久久妲己 | 久久久久99一区二区三区 | 色综合久久久久久久久久久 | 操操操操操 | 久久影院在线观看 | 久久靖品 | 毛毛片在线看 | 一级α片免费看刺激高潮视频 | 久久久成人一区二区免费影院 | 欧美激情第一区 | 日本在线视频免费 | 午夜国产成人 |