欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

struts2支持的結(jié)果類型-創(chuàng)新互聯(lián)

成都創(chuàng)新互聯(lián)公司2013年至今,公司以成都網(wǎng)站制作、網(wǎng)站建設(shè)、系統(tǒng)開發(fā)、網(wǎng)絡(luò)推廣、文化傳媒、企業(yè)宣傳、平面廣告設(shè)計(jì)等為主要業(yè)務(wù),適用行業(yè)近百種。服務(wù)企業(yè)客戶上1000+,涉及國內(nèi)多個(gè)省份客戶。擁有多年網(wǎng)站建設(shè)開發(fā)經(jīng)驗(yàn)。為企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、創(chuàng)意設(shè)計(jì)、宣傳推廣等服務(wù)。 通過專業(yè)的設(shè)計(jì)、獨(dú)特的風(fēng)格,為不同客戶提供各種風(fēng)格的特色服務(wù)。>在struts2-core.jar/struts-default.xml中,我們可以找到關(guān)于result-type的一些配置信息,從中可以看出struts2組件默認(rèn)為我們提供了這 些result-type <result-types> <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/> <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/> <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/> <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /> <!-- Deprecated name form scheduled for removal in Struts 2.1.0. The camelCase versions are preferred. See ww-1707 --> <result-type name="redirect-action" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> <result-type name="plaintext" class="org.apache.struts2.dispatcher.PlainTextResult" /> </result-types> 封裝跳轉(zhuǎn)邏輯   Result的首要職責(zé),是封裝Action層到View層的跳轉(zhuǎn)邏輯。之前我們已經(jīng)反復(fù)提到,Struts2的Action是一個(gè)與Web容器無關(guān)的POJO。所以,在Action執(zhí)行完畢之后,框架需要把代碼的執(zhí)行權(quán)重新交還給Web容器,并轉(zhuǎn)向到相應(yīng)的頁面或者其他類型的View層。而這個(gè)跳轉(zhuǎn)邏輯,就由Result來完成。這樣,好處也是顯而易見的,對Action屏蔽任何Web容器的相關(guān)信息,使得每個(gè)層次更加清晰。   View層的顯示類型非常多,有最常見的JSP、當(dāng)下非常流行的Freemarker/Velocity模板、Redirect到一個(gè)新的地址、文本流、圖片流、甚至是JSON對象等等。所以Result層的獨(dú)立存在,就能夠?qū)@些顯示類型進(jìn)行區(qū)分,并封裝合理的跳轉(zhuǎn)邏輯。   以JSP轉(zhuǎn)向?yàn)槔赟truts2自帶的ServletDispatcherResult中就存在著核心的JSP跳轉(zhuǎn)邏輯: 常用的Result dispatcher Xml代碼 <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>   dispatcher主要用于返回JSP,HTML等以頁面為基礎(chǔ)View視圖,這個(gè)也是Struts2默認(rèn)的Result類型。在使用dispatcher時(shí),唯一需要指定的,是JSP或者HTML頁面的位置,這個(gè)位置將被用于定位返回的頁面: Xml代碼 <result name="success">/index.jsp</result>   而Struts2本身也沒有對dispatcher做出什么特殊的處理,只是簡單的使用Servlet API進(jìn)行forward。   freemarker/ velocity Xml代碼 <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>   隨著模板技術(shù)的越來越流行,使用Freemarker或者Velocity模板進(jìn)行View層展示的開發(fā)者越來越多。Struts2同樣為模板作為Result做出了支持。由于模板的顯示需要模板(Template)與數(shù)據(jù)(Model)的緊密配合,所以在Struts2中,這兩個(gè)Result的主要工作是為模板準(zhǔn)備數(shù)據(jù)。   以Freemarker為例,我們來看看它是如何為模板準(zhǔn)備數(shù)據(jù)的: Java代碼 public void doExecute(String location, ActionInvocation invocation) throws IOException, TemplateException { this.location = location; this.invocation = invocation; this.configuration = getConfiguration(); this.wrapper = getObjectWrapper(); // 獲取模板的位置 if (!location.startsWith("/")) { ActionContext ctx= invocation.getInvocationContext(); HttpServletRequest req= (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST); String base= ResourceUtil.getResourceBase(req); location= base + "/" + location; } // 得到模板 Template template = configuration.getTemplate(location, deduceLocale()); // 為模板準(zhǔn)備數(shù)據(jù) TemplateModel model = createModel(); // 根據(jù)模板和數(shù)據(jù)進(jìn)行輸出 // Give subclasses a chance to hook into preprocessing if (preTemplateProcess(template, model)) { try { // Process the template template.process(model, getWriter()); }finally { // Give subclasses a chance to hook into postprocessing postTemplateProcess(template, model); } } } public void doExecute(String location, ActionInvocation invocation) throws IOException, TemplateException { this.location = location; this.invocation = invocation; this.configuration = getConfiguration(); this.wrapper = getObjectWrapper(); // 獲取模板的位置 if (!location.startsWith("/")) { ActionContext ctx= invocation.getInvocationContext(); HttpServletRequest req= (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST); String base= ResourceUtil.getResourceBase(req); location= base + "/" + location; } // 得到模板 Template template = configuration.getTemplate(location, deduceLocale()); // 為模板準(zhǔn)備數(shù)據(jù) TemplateModel model = createModel(); // 根據(jù)模板和數(shù)據(jù)進(jìn)行輸出 // Give subclasses a chance to hook into preprocessing if (preTemplateProcess(template, model)) { try { // Process the template template.process(model, getWriter()); }finally { // Give subclasses a chance to hook into postprocessing postTemplateProcess(template, model); } } }   從源碼中,我們可以看到,createModel()方法真正為模板準(zhǔn)備需要顯示的數(shù)據(jù)。而之前,我們已經(jīng)看到過這個(gè)方法的源碼,這個(gè)方法所準(zhǔn)備的數(shù)據(jù)不僅包含ValueStack中的數(shù)據(jù),還包含了被封裝過的HttpServletRequest,HttpSession等對象的數(shù)據(jù)。從而使得模板能夠以它特定的語法輸出這些數(shù)據(jù)。 [SPAN]   Velocity的Result也是類似,有興趣的讀者可以順著思路繼續(xù)深究源碼。   redirect Xml代碼 <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>   如果你在Action執(zhí)行完畢后,希望執(zhí)行另一個(gè)Action,有2種方式可供選擇。一種是forward,另外一種是redirect。有關(guān)forward和redirect的區(qū)別,這里我就不再展開,這應(yīng)該屬于Java程序員的基本知識。在Struts2中,分別對應(yīng)這兩種方式的Result,就是chain和redirect。   先來談?wù)剅edirect,既然是重定向,那么源地址與目標(biāo)地址之間是2個(gè)不同的HttpServletRequest。所以目標(biāo)地址將無法通過ValueStack等Struts2的特性來獲取源Action中的數(shù)據(jù)。如果你需要對目標(biāo)地址傳遞參數(shù),那么需要在目標(biāo)地址url或者配置文件中指出: Xml代碼 <!-- The redirect-action url generated will be : /genReport/generateReport.jsp?reportType=pie&width=100&height=100 --> <action name="gatherReportInfo" class="..."> <result name="showReportResult" type="redirect"> <param name="location">generateReport.jsp</param> <param name="namespace">/genReport</param> <param name="reportType">pie</param> <param name="width">${width}</param> <param name="height">${height}</param> </result> </action>   同時(shí),Redirect的Result支持在配置文件中,讀取并解析源Action中ValueStack的值,并成為參數(shù)傳遞到Redirect的地址中。上面給出的例子中,width和height就是ValueStack中的值。   chain Xml代碼 <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>   再來談?wù)刢hain,之前提到,chain其實(shí)只是在一個(gè)action執(zhí)行完畢之后,forward到另外一個(gè)action,所以他們之間是共享HttpServletRequest的。在使用chain作為Result時(shí),往往會(huì)配合使用ChainingInterceptor。有關(guān)ChainingInterceptor,Struts2的Reference說明了其作用:   Struts2 Reference 寫道:If you need to copy the properties from your previous Actions in the chain to the current action, you should apply the ChainingInterceptor. The Interceptor will copy the original parameters from the request, and the ValueStack is passed in to the target Action. The source Action is remembered by the ValueStack, allowing the target Action to access the properties of the preceding Action(s) using the ValueStack, and also makes these properties available to thefinal result of the chain, such as the JSP or Velocity page.   也就是說,ChainingInterceptor的作用是在Action直接傳遞數(shù)據(jù)。事實(shí)上,源Action中ValueStack的數(shù)據(jù)會(huì)被做一次Copy,這樣,2個(gè)Action中的數(shù)據(jù)都在ValueStack中,使得對于前臺來說,通過ValueStack來取數(shù)據(jù),是透明而共享的。chain這個(gè)Result有一些常用的使用情景,這點(diǎn)在Struts2的Reference中也有說明:   Struts2 Reference 寫道:One common use of Action chaining is to provide lookup lists (likefor a dropdown list of states). Since these Actions get put on the ValueStack, their properties will be available in the view. This functionality can also be done using the ActionTag to execute an Action from the display page.   比如說,一張頁面中,你可能有許多數(shù)據(jù)要顯示,而某些數(shù)據(jù)的獲取方式可能被很多不同的頁面共享(典型來說,“推薦文章”這個(gè)小欄目的數(shù)據(jù)獲取,可能會(huì)被很多頁面所共享)。這種情況下,可以把這部分邏輯抽取到一個(gè)獨(dú)立Action中,并使用chain,將這個(gè)Action與主Action串聯(lián)起來。這樣,最后到達(dá)頁面的時(shí)候,頁面始終可以得到每個(gè)Action中的數(shù)據(jù)。   不過chain這種Result,是在使用時(shí)需要慎重考慮的一種Result:   Struts2 Reference 寫道:As a rule, Action Chaining is not recommended. First explore other options, such as the Redirect After Post technique.   而Struts2也做出了理由上的說明:   Struts2 Reference 寫道:Experience shows that chaining should be used with care. If chaining is overused, an application can turn into"spaghetti code". Actions should be treated as a Transaction Script, rather than as methods in a Business Facade. Be sure to ask yourself why you need to chain from one Action to another. Is a navigational issue, or could the logic in Action2 be pushed back to a support class or business facade so that Action1 can call it too? Ideally, Action classes should be asshort as possible. All the core logic should be pushed back to a support class or a business facade, so that Actions only call methods. Actions are best used as adapters, rather than as a class where coding logic is defined.   從實(shí)戰(zhàn)上將,使用chain作為Result也的確存在著上面所說的許多問題,我個(gè)人也是非常不推崇濫用這種Result。尤其是,對于使用Spring和Hibernate的朋友來說,如果你開啟OpenSessionInView模式,那么Hibernate的session是跟隨HttpServletRequest的,所以session在整個(gè)action鏈中共享。這會(huì)為我們的編程帶來極大的麻煩。因?yàn)槲覀冎繦ibernate的session會(huì)保留一份一級緩存,在action鏈中,共享一級緩存無疑會(huì)為你的調(diào)試工作帶來很大的不方便。   所以,謹(jǐn)慎使用chain作為你的Result,應(yīng)該成為一條最佳實(shí)踐。   stream Xml代碼 <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>   StreamResult等價(jià)于在Servlet中直接輸出Stream流。這種Result被經(jīng)常使用于輸出圖片、文檔等二進(jìn)制流到客戶端。通過使用StreamResult,我們只需要在Action中準(zhǔn)備好需要輸出的InputStream即可。 Xml代碼 <result name="success" type="stream"> <param name="contentType">image/jpeg</param> <param name="inputName">imageStream</param> <param name="contentDisposition">filename="document.pdf"</param> <param name="bufferSize">1024</param> </result>   同時(shí),StreamResult支持許多參數(shù),對輸出的Stream流進(jìn)行參數(shù)控制。具體每個(gè)參數(shù)的作用,可以參考:http://struts.apache.org/2.0.14/docs/stream-result.html   其他   Struts2的高度可擴(kuò)展性保證了許多自定義的Result可以通過插件的形式發(fā)布出來。比較著名的有JSONResult,JFreeChartResult等等。有興趣的讀者可以在Struts2的官方網(wǎng)站上找到它們,并選擇合適的加入到你的項(xiàng)目中去。   關(guān)于Result配置簡化的思考   Struts2的Result,解決了“如何從Control層轉(zhuǎn)向View層”的問題。不過看了上面介紹的這些由框架本身實(shí)現(xiàn)的Result,我們可以發(fā)現(xiàn)Result所涉及到的,基本上還停留在為Control層到View層搭建橋梁。   傳統(tǒng)的,我們需要通過配置文件,來指定Action執(zhí)行完畢之后,到底執(zhí)行什么樣的Result。不過在這樣一個(gè)到處呼吁簡化配置的年代,存在著許多方式,可以省略配置:   1. 使用Annotation   Struts2的一些插件提供了@Result和@Results的Annotation,可以通過Annotation來省略XML配置。具體請參考相關(guān)的文檔。   2. Codebehind插件   Struts2自帶了一個(gè)Codebehind插件(Struts2.1以后被合并到了其他的插件中)。Codebehind的基本思想是通過CoC的方式,使用命名約定來確定JSP等資源文件的位置。它通過實(shí)現(xiàn)了XWork的UnknownHandler接口,來實(shí)現(xiàn)當(dāng)Struts2框架無法找到相應(yīng)的Result時(shí),如何進(jìn)行處理的邏輯。具體文檔可以參考:http://struts.apache.org/2.0.14/docs/codebehind-plugin.html   大家可以在上面這兩種方式中任意選擇,國內(nèi)著名的開源倡導(dǎo)者Springside也是采用了上述2種方法。在多數(shù)情況下,使用Codebehind,針對其他的一些Result使用Annotation進(jìn)行配置,這樣可以在一定程度上簡化配置。   不過我本人對使用Annotation簡化配置的評價(jià)不高。因?yàn)閷?shí)際上使用Annotation,只是將原本就非常簡單的配置,從xml文件中移動(dòng)到j(luò)ava代碼中而已。就代碼量而言,本身并沒有減少。   在這里,我也在經(jīng)常在思考,如何進(jìn)行配置簡化,可以不寫Annotation,完全使用CoC的方式來指定Result。Codebehind在CoC方面已經(jīng)做出了榜樣,只是Codebehind無法判別Result的類型,所以它只能支持dispatcher/ freemarker / velocity這三種Result。所以Result的類型的判別,成為了阻礙簡化其配置CoC化的攔路虎。   前一段時(shí)間,曾經(jīng)熱播一部電視劇《暗算》,其中的《看風(fēng)》篇中數(shù)學(xué)家黃依依的一段話給了我靈感:   黃依依寫道:開啟密鎖鑰匙的復(fù)雜化,是現(xiàn)代密碼發(fā)展的趨勢。但這種復(fù)雜化卻受到無線通訊本身的限制,尤其是距離遠(yuǎn)、布點(diǎn)多的呈放射性的無線通訊,一般的密鑰總是要藏在報(bào)文中。   密鑰既然可以藏在報(bào)文中,那么Result的類型當(dāng)然也能夠藏在ResultCode中。 Java代碼 return "success";   這樣一個(gè)簡單的success作為ResultCode,是無法識別成復(fù)雜的Result類型的,我們需要設(shè)計(jì)一套更加有效的ResultCode,同時(shí),Struts2能夠識別這些ResultCode,并得到相應(yīng)的Result類型和Result實(shí)例。這樣,我們就可以借用Codebehind的實(shí)現(xiàn)方式,實(shí)現(xiàn)XWork的UnknownHandler接口,從而達(dá)到我們的目的。例如,我們規(guī)定ResultCode的解析規(guī)則:   success —— 使用codebehind的規(guī)則進(jìn)行JSP,F(xiàn)reemarker模板的尋址   r:/user/list —— 返回一個(gè)redirect的Result,地址為/user/list   c:/user/list —— 返回一個(gè)chain的Result,地址為/user/list   j:user —— 返回一個(gè)JSON的Result,JSONResult的Root對象為user   s:inputStream-text/html —— 返回一個(gè)StreamResult,使用inputStream,并將contentType設(shè)置成text/html   以此類推,大家可以定義自己喜歡的ResultCode的格式,從而簡化配置。有了這樣的規(guī)則,也就有了后來的實(shí)現(xiàn)。具體解析這些ResultCode,并為他們構(gòu)建Result實(shí)例的源碼,大家可以參考我的一個(gè)插件項(xiàng)目LightURL。 redirect和redirectAction chain的區(qū)別 struts2中關(guān)于result的返回類型一般我們是轉(zhuǎn)發(fā)到一個(gè)jsp頁面或者是html頁面等,但是struts2中的result的返回類型還有redirect,redirectAction,chain。對于這三種返回類型之間肯定是有區(qū)別的,下面我們來看看關(guān)于redirect redirectAction chain這三種struts2的返回類型之間的區(qū)別。 當(dāng)使用type=“redirectAction” 或type=“redirect”提交到一個(gè)action并且需要傳遞一個(gè)參數(shù)時(shí)。這里是有區(qū)別的: 使用type=“redirectAction”時(shí),結(jié)果就只能寫Action的配置名,不能帶有后綴:“.action” Xml代碼<action name="Login" class="steven.actions.LoginAction"> <result name="success" type="redirectAction">User?u_id=${loginBean.u_id}</result> </action> <action name="Login" class="steven.actions.LoginAction"> <result name="success" type="redirectAction">User?u_id=${loginBean.u_id}</result> </action> [xml] view plaincopyprint?<action name="Login" class="steven.actions.LoginAction"> <result name="success" type="redirectAction">User?u_id=${loginBean.u_id}</result> </action> <action name="Login" class="steven.actions.LoginAction"> <result name="success" type="redirectAction">User?u_id=${loginBean.u_id}</result> </action> <action name="Login" class="steven.actions.LoginAction"> <result name="success" type="redirectAction">User?u_id=${loginBean.u_id}</result> </action> <action name="Login" class="steven.actions.LoginAction"> <result name="success" type="redirectAction">User?u_id=${loginBean.u_id}</result> </action> 使用type=“redirect”時(shí),結(jié)果應(yīng)是action配置名+后綴名 Xml代碼<action name="Login" class="steven.actions.LoginAction"> <result name="success" type="redirect">User.action?u_id=${loginBean.u_id}</result> </action> [xml] view plaincopyprint?<action name="Login" class="steven.actions.LoginAction"> <result name="success" type="redirect">User.action?u_id=${loginBean.u_id}</result> </action> <action name="Login" class="steven.actions.LoginAction"> <result name="success" type="redirect">User.action?u_id=${loginBean.u_id}</result> </action> redirect:action處理完后重定向到一個(gè)視圖資源(如:jsp頁面),請求參數(shù)全部丟失,action處理結(jié)果也全部丟失。 redirect-action:action處理完后重定向到一個(gè)action,請求參數(shù)全部丟失,action處理結(jié)果也全部丟失。 chain:action處理完后轉(zhuǎn)發(fā)到一個(gè)action,請求參數(shù)全部丟失,action處理結(jié)果不會(huì)丟失。

網(wǎng)站欄目:struts2支持的結(jié)果類型-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://chinadenli.net/article16/gcjgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)全網(wǎng)營銷推廣網(wǎng)站收錄云服務(wù)器做網(wǎng)站網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)