struts2 跳转类型 result type=chain、dispatcher、redirect
轉自:
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>
轉載于:https://my.oschina.net/liangzhenghui/blog/183064
總結
以上是生活随笔為你收集整理的struts2 跳转类型 result type=chain、dispatcher、redirect的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何用代码控制midi数据格式的速度_音
- 下一篇: Struts2 注解中跳转 action