沙漠中的魚

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

          struts重定向

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

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

          一、在struts1 中實現


          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 中,因為執行函數返回結果不再是ActionForward ,而是一個字符串,所以不能再像struts1中那樣跳轉了。

          在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>

          對應的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 ; //給要傳遞的參數賦值

               return SUCCESS;     //默認頁面

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

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

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

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

          }

          }

           

          三、說明


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

          轉載:http://hi.baidu.com/myfreeint/blog/item/d5f42e0122b9a8031c958341.html
          主站蜘蛛池模板: 旺苍县| 离岛区| 定西市| 余干县| 大方县| 汉川市| 和顺县| 东台市| 盐池县| 东莞市| 南城县| 巴林左旗| 栾城县| 永修县| 温宿县| 竹山县| 周至县| 句容市| 蓬安县| 湘西| 宜黄县| 天峨县| 云林县| 阜宁县| 诸城市| 桂阳县| 巴东县| 内黄县| 谷城县| 电白县| 绥滨县| 东港市| 调兵山市| 顺平县| 高安市| 称多县| 蓝山县| 磴口县| 罗山县| 手游| 西林县|