沙漠中的魚

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

          struts重定向

          Posted on 2009-03-19 11:19 沙漠中的魚 閱讀(2215) 評論(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
          主站蜘蛛池模板: 秦皇岛市| 本溪| 武威市| 新密市| 阿城市| 六枝特区| 洪湖市| 紫阳县| 科技| 蒲江县| 巩义市| 襄樊市| 胶南市| 浏阳市| 梧州市| 金阳县| 韶关市| 金堂县| 都江堰市| 铜梁县| 蓬莱市| 奈曼旗| 花莲县| 兴仁县| 漳平市| 邯郸市| 吉木乃县| 绍兴县| 克拉玛依市| 甘肃省| 修水县| 和平区| 湘阴县| 昌黎县| 九江市| 灌阳县| 彩票| 宁陵县| 安顺市| 鲁甸县| 隆尧县|