沙漠中的魚

          欲上天堂,先下地獄
          posts - 0, comments - 56, trackbacks - 0, articles - 119
            BlogJava :: 首頁 ::  :: 聯(lián)系 :: 聚合  :: 管理

          struts重定向

          Posted on 2009-03-19 11:19 沙漠中的魚 閱讀(2222) 評論(0)  編輯  收藏 所屬分類: 開源框架
          struts2 的重定向和struts1 在使用方法上有所不同。

          如在一個登錄的action中驗證成功后,重定向為顯示用戶信息的action: showInfo.do

          一、在struts1 中實現(xiàn)


          public class LoginAction extends Action {

          public ActionForward execute(ActionMapping mapping, ActionForm form,
              HttpServletRequest request, HttpServletResponse response) {
             
               //一些處理……

               //重定向
               ActionForward forward = new ActionForward("showInfo.do");
               forward.setRedirect(true);
               return forward ;
          }
          }


          二、在struts2 中,因為執(zhí)行函數(shù)返回結(jié)果不再是ActionForward ,而是一個字符串,所以不能再像struts1中那樣跳轉(zhuǎn)了。

          在struts2中,重定向要在struts.xml中配置:

          <action name="login" class="LoginAction">
             <result name="success" type="velocity">/pages/logok.vm</result>
             <result name="redirect_1" type="redirect">showInfo.do</result>
             <result name="redirect_2" type="redirect">showInfo.do?name=yangzi</result>
             <result name="redirect_3" type="redirect">showInfo.do?name=${name}</result>
             <result name="redirect_4" type= "redirect">
                        <param name="actionName">showInfo</param>
                        <param name="name">${name}</param>
              </result>   

          </action>

          對應(yīng)的LoginAction:

          public class LoginAction extends ActionSupport{

          String name;

          public String getName() {
             return name;
          }

          public void setName(String name) {
             this.name = name;
          }


          public String execute() throws Exception {

               //一些處理……

               name=xiaowang ; //給要傳遞的參數(shù)賦值

               return SUCCESS;     //默認頁面

             //return "redirect_1" ; //重定向(不帶參數(shù)) showInfo.do

             //return "redirect_2" ; //重定向(帶固定參數(shù)yangzi) showInfo.do?name=yangzi

             //重定向(帶動態(tài)參數(shù),根據(jù)struts.xml的配置將${name}賦值為xiaowang)最后為 showInfo.do?name=xiaowang  
             // return "redirect_3" ;

          //return "redirect_4" ; //這個是重定向到 一個action

          }

          }

           

          三、說明


          struts2 重定向分重定向到url和重定向到一個action。
          實現(xiàn)重定向,需在struts.xml中定義返回結(jié)果類型。
          type="redirect" 是重定向到一個URL。type="redirect-action" 是重定向到一個action。
          參數(shù)也是在這里指定,action中所做的就是給參數(shù)賦值,并return 這個結(jié)果。
          個人認為:由于大家極度抱怨“action臃腫”,所以struts2中盡量減少了action中的代碼。

          轉(zhuǎn)載:http://hi.baidu.com/myfreeint/blog/item/d5f42e0122b9a8031c958341.html
          主站蜘蛛池模板: 甘南县| 德江县| 镶黄旗| 古交市| 阳信县| 万安县| 武陟县| 桐庐县| 长岭县| 德昌县| 沽源县| 大余县| 荥经县| 房产| 桂林市| 静宁县| 阿城市| 威海市| 新邵县| 新兴县| 黄冈市| 台前县| 山东省| 罗源县| 辽阳县| 朝阳市| 淮北市| 巴林右旗| 龙海市| 讷河市| 伊吾县| 炉霍县| 大厂| 澎湖县| 罗甸县| 灌云县| 阿勒泰市| 茂名市| 余江县| 治县。| 岢岚县|