struts2 跳轉類型 result type=chain、dispatcher、redirect(redirect-action)

dispatcher 爲默認跳轉類型,用於返回一個視圖資源(如: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>

用於頁面轉發,頁面跳轉過程一直是同一個線程,Action中的數據一直保存在。
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把一個http返回碼(SUCCESS)以及返回的頁面位置一起重新發給web服務器,容納後由web服務器產生一個新的HTTP請求,就會產生一個新的線程,保存在原來Action執行的線程中的數據就無法訪問。
所以,result需要包含Action的數據,那麼redirect不是一個可行的辦法。因爲新的HTTP請求時在Servlet容器的新的線程中處理的,ActionContext中的所有狀態都不會存在。

處理方法:

(方法一):
<result name="topic" type="redirect">/topicAction!findTopics.do?topicId=${topicId}</result>
(方法二):
<result name="topic" type="redirect-action">
<param name="actionName">findTopics</param>
<param name="topicId">${topicId}</param>
</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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章