天天看點

struts2--結果集轉發類型

struts2--結果集轉發類型

    <result name="success" type="轉發類型">/xxx.jsp</result> 

    在"struts2-core-2.5.5.jar"==>"struts-default.xml"下的系統預設配置

        <result-types>

            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>

            <result-type name="dispatcher" class="org.apache.struts2.result.ServletDispatcherResult" default="true"/>

            <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>

            <result-type name="httpheader" class="org.apache.struts2.result.HttpHeaderResult"/>

            <result-type name="redirect" class="org.apache.struts2.result.ServletRedirectResult"/>

            <result-type name="redirectAction" class="org.apache.struts2.result.ServletActionRedirectResult"/>

            <result-type name="stream" class="org.apache.struts2.result.StreamResult"/>

            <result-type name="velocity" class="org.apache.struts2.result.VelocityResult"/>

            <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>

            <result-type name="plainText" class="org.apache.struts2.result.PlainTextResult" />

            <result-type name="postback" class="org.apache.struts2.result.PostbackResult" />

        </result-types>

   chain  轉發到一個action,而不是頁面

   這個結果調用其他action,完成它自己定義的攔截器堆棧和結果。隻能請求action,如果請求視圖資源會報錯。

   需要注意的就是與redirect的差別,請求轉發是還在目前請求,而redirect會響應一次浏覽器然後浏覽器再根據響應請求重定向的資源,

   dispatcher  (預設類型,如果沒有配置類型預設就是dispatcher,轉發到一個頁面)

   當一個請求到來,伺服器直接轉發到另一個頁面,不能是另一個action。由于這個過程在伺服器内部完成,用戶端(浏覽器)并不知道,

   是以在位址欄不會顯示真實通路的頁面,而顯示都是所請求的action的位址。在servlet中相當與forword轉發。

   包括或轉發到一個視圖(通常是一個jsp)。在背景Struts2将使用一個RequestDispatcher,目标servlet/JSP接收相同的request/response對象作為原始的servlet或JSP。 

   是以,可以使用request.setAttribute()傳遞資料- - - Struts的action是可用的。如果請求action會找不到資源。

    redirectAction 

    重定向至Action,完成與它自己的攔截器堆棧和結果。

    相對于redirect來說,redirectAction隻能請求action,如果請求視圖資源會報錯,

    然後還有個小差別就是redirectAction會為url添加.action字尾而redirect不會,但是兩者都可以通過url傳參

    redirect  重定向到一個頁面或另一個action或一個網址

   當一個請求到來,服務端将實際位址response給浏覽器,然後浏覽器重新發起請求,這個過程,浏覽器是知道通路的頁面的實際位址的,

   是以在浏覽器的位址欄顯示的是實際通路的jsp頁面位址。但是這種類型不能重定向到一個action.

    讓用戶端請求另外的網絡資源,可以為action,也可以為視圖資源。 

    文檔上是這麼解釋的: 

    調用{ @link HttpServletResponse # sendRedirect(String)sendRedirect }方法到指定的位址。 響應是告訴重定向浏覽器到指定的位址(一個新的請求從用戶端)。

    這樣做的結果意味着action(action instance, action errors, field errors等)隻是執行失敗,不再可用。

    這是因為action是一個單線程模式(single-thread model)。唯一的傳參方法是通過會話或用OGNL表達式,url參數(url ?名稱=值)

    httpheader 

    可以通過設定HTTP headers和status的值來發送錯誤資訊給用戶端。 

他的參數有這些: 

status - the http servlet response status code that should be set on a response. 

parse - true by default. If set to false, the headers param will not be parsed for Ognl expressions. 

headers - header values. 

error - the http servlet response error code that should be set on a response. 

errorMessage - error message to be set on response if 'error' is set. 

    stream :這個傳回類型主要用作下載下傳檔案或者在浏覽器上顯示PDF等文檔 

    plainText :響應以plain形式傳回給用戶端,相當于response.setContentType("text/plain; charset="+charSet); 

    velocity :使用Servlet容器的JspFactory,這個結果模拟一個JSP執行環境,然後顯示一個Velocity模闆,将直接傳輸到Servlet輸出。 

    freemarker :呈現一個視圖使用Freemarker模闆引擎。。 

    xslt :調用一個xslt檔案并解析執行。

    Tiles是一個模闆架構被設計來輕松地允許建立web應用程式的頁面具有一緻的外觀和感覺。它可以用于頁面群組件化裝飾。 

    特性:支援在Freemarker,JSP,Velocity使用Tiles 

繼續閱讀