歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Struts2結果類型(Result Types)

Struts2結果類型(Result Types)

日期:2017/3/1 9:26:46   编辑:Linux編程

在struts2-core-2.0.11.2.jar根目錄下的struts-default.xml中配置了struts2提供的一系列結果類型
struts2的結果類型實現了com.opensymphony.xwork2.Result接口
type支持的結果類型:
chain:用來處理Action鏈
chart:整合JFreeChart的結果類型
dispatcher:用來轉向頁面,通常處理JSP
freemaker:處理FreeMarker模板
httpheader:控制特殊HTTP行為的結果類型
jasper:用於JasperReports整合的結果類型
jsf:JSF整合的結果類型
redirect:重定向到一個URL
redirect-action:重定向到一個Action
stream:向浏覽器發送InputSream對象,通常用來處理文件下載,還可用於返回AJAX數據
tiles:與Tiles整合的結果類型
velocity:處理Velocity模板
xslt:處理XML/XLST模板
plaintext:顯示原始文件內容,例如文件源代碼

dispatcher 結果類型為缺省的result類型,用於返回一個視圖資源(如:jsp)
Xml代碼 :

<result name="success">/main.jsp</result>
<result name="success">/main.jsp</result>
以上寫法使用了兩個默認,其完整的寫法為:
<result name="success" type="dispatcher">
<param name="location">/maini.jsp</param>
</result>
location只能是頁面,不能是另一個action(可用type="chain"解決)。

redirect 結果類型用於重定向到一個頁面,另一個action或一個網址。
Xml代碼:

<result name="success" type="redirect">aaa.jsp</result>
<result name="success" type="redirect">bbb.action</result>
<result name="success" type="redirect">www.baidu.com</result>

redirect-action 結果類型使用ActionMapperFactory提供的ActionMapper來重定向請求到另外一個action
Xml代碼:

<result name="err" type="redirect-action">
<param name="actionName">重定向的Action名</param>
<param name="namespace">重定向Action所在的名字空間</param>
</result>
redirect和redirect-action兩種結果類型在使用上其實並沒有什麼區別,只是寫法不同而已。

chain 用於把相關的幾個action連接起來,共同完成一個功能。
Xml代碼:

<action name="step1" class="test.Step1Action">
<result name="success" type="chain">step2.action</result>
</action>
<action name="step2" class="test.Step2Action">
<result name="success">finish.jsp</result>
</action>
處於chain中的action屬於同一個http請求,共享一個ActionContext

plaintextj 結果類型用於直接在頁面上顯示源代碼

Xml代碼:

<result name="err" type="plaintext">
<param name="location">具體的位置</param>
<param name="charSet">字符規范(如GBK)</param>
</result>

Struts2學習筆記-Value Stack(值棧)和OGNL表達式 http://www.linuxidc.com/Linux/2015-07/120529.htm

struts2文件上傳(保存為BLOB格式) http://www.linuxidc.com/Linux/2014-06/102905.htm

Struts2的入門實例 http://www.linuxidc.com/Linux/2013-05/84618.htm

Struts2實現ModelDriven接口 http://www.linuxidc.com/Linux/2014-04/99466.htm

遇到的Struts2文件下載亂碼問題 http://www.linuxidc.com/Linux/2014-03/98990.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

Struts2 注解模式的幾個知識點 http://www.linuxidc.com/Linux/2013-06/85830.htm

Struts 的詳細介紹:請點這裡
Struts 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved