struts2中的Result Type簡介

1、簡介

        在默認時,<result>標籤的type屬性值是“dispatcher”(實際上就是轉發,forward)。開發人員可以根據自己的需要指定不同的類型,如redirect、stream等。如下面代碼所示:

        <result name="save" type="redirect">
        /result.jsp
        </result>
        這此result-type可以在struts2-core-2.0.11.1.jar包或struts2源代碼中的struts-default.xml文件中找到,在這個文件中找到<result-types>標籤,所有的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>

2、詳解

2.1 dispatcher
        用來轉向頁面,通常處理JSP
        org.apache.struts2.dispatcher.ServletDispatcherResult
2.2 freemaker
        處理FreeMarker模板
        org.apache.struts2.views.freemarker.FreemarkerResult
2.3 httpheader
        控制特殊HTTP行爲的結果類型
        org.apache.struts2.dispatcher.HttpHeaderResult
2.4 stream
        向瀏覽器發送InputStream對象,通常用來處理文件下載,還可用於返回AJAX數據
        org.apache.struts2.dispatcher.StreamResult
        StreamResult等價於在Servlet中直接輸出Stream流。這種Result被經常使用於輸出圖片、文檔等二進制流到 客戶端。通過使用StreamResult,我們只需要在Action中準備好需要輸出的InputStream即可。
配置:
        <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>
        同時,StreamResult支持許多參數,對輸出的Stream流進行參數控制。
2.5 velocity
        處理Velocity模板
        org.apache.struts2.dispatcher.VelocityResult
        隨着模板技術的越來越流行,使用Freemarker或者Velocity模板進行View層展示的開發者越來越多。Struts2同樣爲模板作爲Result做出了支持。由於模板的顯示需要模板(Template)與數據(Model)的緊密配合,所以在Struts2中,這兩個Result的主要工作是爲模板準備數據。
2.6 xslt
        處理XML/XLST模板
        org.apache.struts2.views.xslt.XSLTResult
2.7 plainText
        顯示原始文件內容,例如文件源代碼
        org.apache.struts2.dispatcher.PlainTextResult
2.8 chain
        用來處理Action鏈
        com.opensymphony.xwork2.ActionChainResult
        chain其實只是在一個action執行完畢之後,forward到另外一個action,所以他們之間是共享HttpServletRequest的。在使用chain作爲Result時,往往會配合使用ChainingInterceptor。ChainingInterceptor的作用是在Action直接傳遞數據。事實上,源Action中ValueStack的數據會被做一次Copy,這樣,2個Action中的數據都在ValueStack中,使得對於前臺來說,通過ValueStack來取數據,是透明而共享的。比如說,一張頁面中,你可能有許多數據要顯示,而某些數據的獲取方式可能被很多不同的頁面共享(典型來說,“推薦文章”這個小欄目的數據獲取,可能會被很多頁面所共享)。這種情況下,可以把這部分邏輯抽取到一個獨立Action中,並使用chain,將這個Action與主Action串聯起來。這樣,最後到達頁面的時候,頁面始終可以得到每個Action中的數據。
        從實戰上講,使用chain作爲Result也的確存在着上面所說的許多問題,我個人也是非常不推崇濫用這種Result。尤其是,對於使用Spring和Hibernate的朋友來說,如果你開啓OpenSessionInView模式,那麼Hibernate的session是跟隨HttpServletRequest的,所以session在整個action鏈中共享。這會爲我們的編程帶來極大的麻煩。因爲我們知道Hibernate的session會保留一份一級緩存,在action鏈中,共享一級緩存無疑會爲你的調試工作帶來很大的不方便。
所以,謹慎使用chain作爲你的Result,應該成爲一條最佳實踐。
2.9 redirect
        重定向到一個URL
        org.apache.struts2.dispatcher.ServletRedirectResult
        如果你在Action執行完畢後,希望執行另一個Action,有2種方式可供選擇。一種是forward,另外一種是redirect。有關forward和redirect的區別,這裏我就不再展開,這應該屬於Java程序員的基本知識。在Struts2中,分別對應這兩種方式的Result,就是chain和redirect。
先來談談redirect,既然是重定向,那麼源地址與目標地址之間是2個不同的HttpServletRequest。所以目標地址將無法通過ValueStack等Struts2的特性來獲取源Action中的數據。
        同時,Redirect的Result支持在配置文件中,讀取並解析源Action中ValueStack的值,併成爲參數傳遞到Redirect的地址中。
2.10 redirectAction
        重定向到一個Action

        org.apache.struts2.dispatcher.ServletActionRedirectResult


參考文獻:百度百科

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章