Result結果類型詳解


1. 配置Result

在 struts.xml 文件中,<result> 元素用於配置 Result 邏輯視圖與物理視圖之間的映射關系,它有兩個可選屬性 name 和 type。其中,name 屬性用於指定邏輯視圖的名稱,默認值為 success;type 屬性用於指定返回的視圖資源的類型,不同的類型代表不同的結果輸出,它的默認值是 dispatcher。

<action name="loginAction" class="com.mengma.action.LoginAction">
    <result name="success" type="dispatcher">
        <param name="location">/success.jsp</param>
    </result>
</action>
<!-- Action 配置了一個 name 為 success 的 Result 映射,該映射的值可以是 JSP 頁面,也可以是一個 Action 的 name 值;
   這里使用 param 子元素為其指定了 Result 映射對應的物理視圖資源為 success.jsp。
-->

<param> 子元素的 name 屬性有兩個值:

  • location:指定該邏輯視圖所對應的實際視圖資源。
  • parse:指定在邏輯視圖資源名稱中是否可以使用 OGNL(對象圖導航語言)表達式。默認值為 true,表示可以使用,如果設為 false,則表示不支持。

簡化上面的代碼:

<action name="loginAction" class="com.mengma.action.LoginAction">
    <result>/success.jsp</result>
</action>

需要注意的是,在 Result 配置中指定實際資源位置時,可以使用絕對路徑,也可以使用相對路徑。

  • 絕對路徑以斜杠“/”開頭,例如<result>/success.jsp</result>,相當於當前 Web 應用程序的上下文路徑。
  • 相對路徑不以斜杠“/”開頭,例如 <result>success.jsp</result>,相當於當前執行的 Action 路徑。

2. 預定義的結果類型

在使用 Struts2 框架編寫項目時,當框架調用 Action 對請求進行處理后,就要向用戶呈現一個結果視圖。在 Struts2 中,預定義了多種 ResultType(結果類型)展示結果視圖。

一個結果類型就是實現了 com.opensymphony.xwork2.Result 接口的類,Struts2 把內置的 <result-type> 都放在 struts-default 包中,struts-default 包就是配置包的父包,這個包定義在 struts2-core-2.3.24.jar 包的根目錄下的 struts-default.xml 文件中,在該文件中可以找到相關的 <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" />
    <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
</result-types>

每個 <result-type> 元素都是一種視圖技術或者跳轉方式的封裝,其中 name 屬性指出在 <result> 元素中如何引用這種視圖技術或者跳轉方式,它對應着 <result> 元素的 type 屬性。class 屬性表示這種結果類型的對應類。

Struts2 中預定義的 ResultType

 屬性  說明
 chain  用於處理Action鏈,被跳轉的Action中仍能獲取上一個頁面的數據的值,如request信息
 dispatcher  跳轉頁面,用於轉向頁面,通常處理JSP,是默認的結果類型
 freemarker  用於整合FreeMarker
 httpheader  用於處理特殊的HTTP行為結果類型
 redirect  重定向到一個URL,跳轉的頁面中丟失數據
 redirectAction  重定向到一個Action,跳轉的頁面中丟失傳遞信息
 stream  向瀏覽器發送InputStream對象,通常用於處理文件下載,還可以用於Ajax數據
 velocity   用於整合Velocity模板結果類型 
 xslt   用於整合XML/XSLT結果類型 
 plainText   顯示原始文件內容,如文件源代碼 
 postback  使當前請求參數以表單形式提交

Struts2 中預定義的全部 11 種結果類型,其中 dispatcher 是默認的結果類型,主要用於與 JSP 整合,dispatcher 和 redirect 是比較常用的結果類型。

需要注意的是,redirect 與 dispatcher 結果類型非常相似,所不同的是 dispatcher 結果類型是將請求轉發到 JSP 視圖資源,而 redirect 結果類型是將請求重定向到 JSP 視圖資源。如果重定向了請求,那么將丟失所有參數,包括 Action 的處理結果。

3. dispatcher結果類型

dispatcher 是 Struts2 的默認結果類型,它用於表示轉發到指定結果資源。

由於 Struts2 在后台使用 RequestDispatcher 的 forward() 方法轉發請求,所以在用戶的整個請求/響應過程中,保持的是同一個請求對象,即目標 JSP/Servlet 接收到的請求/響應對象與最初的 JSP/Servlet 的請求/響應對象相同。

<param> 子元素:

<result name="success" type="dispatcher">
    <param name="location">/success.jsp</param>
    <param name="parse">true</param>
</result>

● location 參數用於指定 Action 執行完畢后要轉向的目標資源;

● parse 參數是一個布爾類型的值,默認是 true,表示解析 location 參數中的 OGNL 表達式,如果為 false,則不解析。

4. redirect結果類型

redirect 結果類型用於重定向到指定的結果資源,該資源可以是 JSP 文件,也可以是 Action 類。

使用 redirect 結果類型時,系統將調用 HttpServletResponse 的 sendRedirect() 方法將請求重定向到指定的 URL;

在使用 redirect 時,用戶要完成一次和服務器之間的交互,瀏覽器需要發送兩次請求。

 

使用 redirect 結果類型的工作過程:

1)瀏覽器發出一個請求,Struts2框架調用對應的Action實例對請求進行處理。

2)Action返回success結果字符串,Struts2框架根據這個結果選擇對應的結果類型,這里使用的是redirect結果類型。

3)ServletRedirectResult在內部使用HttpServletResponse的sendRedirect()方法將請求重新定向到目標資源。

4)瀏覽器重新發起一個針對目標資源的新請求。

5)目標資源作為響應呈現給用戶。

將成功登錄的結果類型設置為 redirect,它表示當 Action 處理請求后會重新生成一個請求。將錯誤的結果類型設置為 dispatcher,這也是結果類型的默認值。

<action name="login" class="com.mengma.action.LoginAction">
    <result name="success" type="redirect">/success.jsp</result>
    <result name="error" type="dispatcher">/error.jsp</result>
</action>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM