(轉(zhuǎn)載)關(guān)于paramsPrepareParamsStack

          原帖地址:
          http://hi.baidu.com/%CC%AB%C6%BD%D1%F31986/blog/item/110b13b1384e805e08230259.html
          轉(zhuǎn)貼

          paramsPrepareParamsStack在Struts 2.0中是一個很奇妙的interceptor stack,以至于很多人疑問為何不將其設(shè)置為默認(rèn)的interceptor stack。paramsPrepareParamsStack主要解決了ModelDriven和Preparable的配合問題,從字面上理解來說, 這個stack的攔截器調(diào)用的順序為:首先params,然后prepare,接下來modelDriven,最后再params。Struts 2.0的設(shè)計上要求modelDriven在params之前調(diào)用,而業(yè)務(wù)中prepare要負(fù)責(zé)準(zhǔn)備model,準(zhǔn)備model又需要參數(shù),這就需要在 prepare之前運行params攔截器設(shè)置相關(guān)參數(shù),這個也就是創(chuàng)建paramsPrepareParamsStack的原因。流程如下:
             1. params攔截器首先給action中的相關(guān)參數(shù)賦值,如id  
             2. prepare攔截器執(zhí)行prepare方法,prepare方法中會根據(jù)參數(shù),如id,去調(diào)用業(yè)務(wù)邏輯,設(shè)置model對象
             3. modelDriven攔截器將model對象壓入value stack,這里的model對象就是在prepare中創(chuàng)建的
             4. params攔截器再將參數(shù)賦值給model對象
             5. action的業(yè)務(wù)邏輯執(zhí)行 依據(jù)此stack,一個action的代碼通常如下

          public class UserAction extends ActionSupport implements ModelDriven, Preparable {
              private User user;
              private int id;
              private UserService service; // user business service

              public void setId(int id) {
                  this.id = id;
              }

              /**
               * create a new user if none exists, otherwise load the user with the
               * specified id
               */
              public void prepare() throws Exception {
                  if (id == 0) {
                      user = new User();
                  } else {
                      user = service.findUserById(id);
                  }
              }

              public Object getModel() {
                  return user;
              }

              /**
               * create or update the user and then view the created user
               */
              public String update() {
                  if (id == 0) {
                      service.create(user);
                  } else {
                      service.update(user);
                  }
                  return "redirect";
              }

              /**
               * delete the user and go to a default home page
               */
              public String delete() {
                  service.deleteById(id);
                  return "home";
              }

              /**
               * show the page allowing the user to view the existing data
               */
              public String view() {
                  return "view";
              }

              /**
               * show the page allowing the user to view the existing data and change the
               * values
               */
              public String edit() {
                  return "input";
              }

          在上述代碼中,edit和view都不需要根據(jù)id再為界面準(zhǔn)備數(shù)據(jù),因為prepare方法已經(jīng)準(zhǔn)備好了model,這些方法很簡單。對于update 方法,prepare首先會從數(shù)據(jù)庫中加載數(shù)據(jù),然后params攔截器會將參數(shù)值付給model,在update直接更新就可以,不會出現(xiàn)數(shù)據(jù)被亂更新 的情況。象Hibernate框架,會判斷哪些字段更新了,然后進(jìn)行更新,性能也不會損失。
          通過paramsPrepareParamsStack可以讓流程更明確,代碼更簡潔,也更利于大家的交流。

          posted on 2011-11-16 15:39 AK47 閱讀(447) 評論(0)  編輯  收藏 所屬分類: Structs

          <2011年11月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 平顶山市| 凭祥市| 博兴县| 阿勒泰市| 郁南县| 中山市| 扎囊县| 吴江市| 嘉黎县| 栾城县| 文水县| 察雅县| 甘泉县| 铁岭市| 阳高县| 萝北县| 鹤岗市| 彰武县| 洪洞县| 高密市| 东城区| 隆德县| 兴义市| 阳朔县| 阿巴嘎旗| 樟树市| 乌什县| 青海省| 柳河县| 耿马| 台南县| 恩施市| 嘉义县| 双辽市| 沂水县| 东兰县| 合阳县| 龙陵县| 中宁县| 八宿县| 阿坝县|