Struts2中redirect基本的經驗之談

1. redirect:action處理完後重定向到一個視圖資源(如:jsp頁面),請求參數全部丟失,action處理結果也全部丟失。


2. redirect-action:action處理完後重定向到一個action,請求參數全部丟失,action處理結果也全部丟失。

 
3. chain:action處理完後轉發到一個action,請求參數全部丟失,action處理結果不會丟失。

 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

Redirect Action Result:


   這個Result使用ActionMapperFactory提供的ActionMapper來重定位瀏覽器的URL來調用指定的action和(可選的)namespace.
   這個Result比ServletRedirectResult要好.因爲你不需要把URL編碼成xwork.xml中配置的ActionMapper提供的模式.
這就是說你可以在任意點上改變URL模式而不會影響你的應用程序. 因此強烈推薦使用這個Result而不是標準的redirect result來解決重定位到某個action的情況.


ActionName (默認) - 重定位到的action名 namespace - action的名稱空間. 如果爲null,則爲當前名稱空間

 


Redirect Result:


    調用{@link HttpServletResponse#sendRedirect(String) sendRedirect}方法來轉到指定的位置. 
HTTP響應被告知使瀏覽器直接跳轉到指定的位置(產生客戶端的一個新請求). 這樣做的結果會使剛剛執行的action(包括action實例,action中的錯誤消息等)丟失, 不再可用. 
這是因爲action是建立在單線程模型基礎上的. 傳遞數據的唯一方式就是通過Session或者可以爲Ognl表達式的web參數(url?name=value)location (默認) - action執行後跳轉的地址. 
parse - 默認爲true. 如果設置爲false, location參數不會被當作Ognl表達式解析.

 

Xml代碼  收藏代碼
  1. <result name="success" type="redirect">/displayCart.action?userId=${userId}</result>  
  2.   
  3. <action   name"delete "   class"com.zeng.action.UserManageAction "   method"delete ">   
  4.     <result   type"redirect "> list.action?pageBean.pageNumber=${pageBean.pageNumber} </result>   
  5. </action>  

 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

    今天在用struts2在做項目時候,從一個action我想跳轉到另一個action,並且呢得帶上值。說說我的做法吧,首先你得在你的第一個action中這個id必須要有set、get方法。跳轉時你的struts.xml:


    (方法一):

 

Xml代碼  收藏代碼
  1. <result name="topic" type="redirect">/topicAction!findTopics.do?topicId=${topicId}</result>   

 

   (方法二):

Xml代碼  收藏代碼
  1. <result name="topic" type="redirect-action">   
  2. <param name="actionName">findTopics</param>   
  3. <param name="topicId">${topicId}</param>   
  4. </result>  

 

    如果是多個參數的話,繼續再加幾個<param>就行了,對於(方法一)如果是多個參數的怎麼辦?

Xml代碼  收藏代碼
  1. <result name="topic" type="redirect">/topicAction!findTopics.do?topicId=${topicId}&amp;elementId=${elementId}</result>   

 

 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

    使用redirect重置鏈接需要後綴名,使用了redirect——action就不能使用了,就例如使用chain一樣,只需要寫action的配置名,如果加入後綴名.action,就會報出異常,action未配置正確。

 

    鍵字: struts2 redirect-action 傳遞 參數


    在做一個系統,使用struts2框架,在提交一個請求後,將獲取的數據對象再要生成一個序列號,爲了防止刷新生成冗餘序列號,就在請求處理完成後,直接重定向到顯示該信息的action中:

 

Xml代碼  收藏代碼
  1. <action name="enterpreinfo" class="preinfoBusinessAction" method="enterPreinfoSub">  
  2. <result name="success" type="redirect-action">  
  3.      showpreinfo?preinfo.order_number=${preinfo.order_number}&amp;preinfo.company_name=${preinfo.company_name}  
  4. </result>  
  5. <result name="error" type="redirect">  
  6. <param name="location">/error.jsp</param>  
  7. </result>  
  8. </action>  

 

    因爲使用了redirect-action,所以要注意不能將 showpreinf?preinfo.order_number=${preinfo.order_number}寫成 showpreinf.action?preinfo.order_number=${preinfo.order_number}

 

    在這個配置文件裏,多個參數的連接符使用了"&amp;",但XML的語法規範,應該使用"&amp;"代替"&",原理和HTML中的轉義相同,開始沒有注意,在struts分析配置文件時,總是報出這樣的錯誤:

 

    The reference to entity "preinfo" must end with the ';' delimiter.

 

 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

    註解方式:

 

Java代碼  收藏代碼
  1. @Results({   
  2.     @Result(name="input", type="redirectAction", params = {"actionName" , "resend"})   
  3. })  

 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

    struts2.1.6無論是xml還是annotation配置redirectAction時,如果要傳一些參數,可是這些參數在ServletActionRedirectResult並沒有聲明,這時ognl會拋異常出來。但實際上傳值是成功的。詳見struts2的jira:


    例:

 

Java代碼  收藏代碼
  1. @Results({     
  2.     @Result(name="reload",type="redirectAction"   
  3.     ,params={"actionName","hello_world"   
  4.             ,"namespace","/center/part1"   
  5.             ,"id","09"   
  6.             ,"count","90"})     
  7. })  
  8.   
  9. @Results({  
  10.     @Result(name="reload",type="redirectAction"  
  11. ,params={"actionName","hello_world"  
  12.        ,"namespace","/center/part1"  
  13.        ,"id","09"  
  14.        ,"count","90"})  
  15. })  

  


 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

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

Java代碼  收藏代碼
  1. <result name="success">/main.jsp</result>   
  2. <result name="success">/main.jsp</result>   

 

    以上寫法使用了兩個默認,其完整的寫法爲:

 

Java代碼  收藏代碼
  1. <result name="success" type="dispatcher">   
  2.      <param name="location">/maini.jsp</param>   
  3. </result>   

 
    location只能是頁面,不能是另一個action(可用type="chain"解決)。

 

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

Java代碼  收藏代碼
  1. <result name="success" type="redirect">aaa.jsp</result>   
  2. <result name="success" type="redirect">bbb.action</result>   
  3. <result name="success" type="redirect">www.baidu.com</result>  

 

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

   

Xml代碼  收藏代碼
  1. <result name="err" type="redirect-action">   
  2.     <param name="actionName">重定向的Action名</param>   
  3.      <param name="namespace">重定向Action所在的名字空間</param>   
  4. </result>  

 

    redirect和redirect-action兩種結果類型在使用上其實並沒有什麼區別,只是寫法不同而已。

 

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

 

Java代碼  收藏代碼
  1. <action name="step1" class="test.Step1Action">   
  2.      <result name="success" type="chain">step2.action</result>   
  3. </action>   
  4. <action name="step2" class="test.Step2Action">   
  5. <result name="success">finish.jsp</result>   
  6. </action>   

 

   處於chain中的action屬於同一個http請求,共享一個ActionContext

 

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

 

Xml代碼  收藏代碼
  1. <result name="err" type="plaintext">   
  2.     <param name="location">具體的位置</param>   
  3.     <param name="charSet">字符規範(如GBK)</param>   
  4. </result>  

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章