posts - 431,  comments - 344,  trackbacks - 0

          在Spring MVC體系里,已經提供了bind(request,command)函數進行Bind and Validate工作。
            但因為默認的bind()函數是拋出Servlet異常,而不是返回以數組形式保存錯誤信息的BindingResult對象供Controller處理 所以BaseController另外實現了一個bindObject函數:

          BindException  bindObject(ServletRequest request, Object command)
          1.Bind And Validate的完整使用代碼:
               public BindingResult bindBook(HttpServletRequest request, Book book) throws Exception
              {
           Integer category_id = new Integer(request.getParameter("category.id"));
                  book.setCategory(bookManager.getCategory(category_id));
                  binder.setDisallowedFields(new String[]{"category"});
          addValidator(new BookValiator());
                  return bindObject(request, book);
          }
            其中第1-3句是綁定不能自動綁定的Category對象,(另外一個方案是實現Category的PropertityEditor,并注冊,不過這太不實際了)并命令binder忽略這些已被手工綁定的field.
                 注意,如果不忽略,binder綁定時則很有可能出錯。

            第4句增加validator。

            第5句執行Bind and Validate。

          不過,我們一般會重載preBind()函數來完成1-3句的操作。逐一
               而且springmodules+ common-validator已經提供了默認的幾種Validator和在XML節點配置默認注入的框架,只有自己寫了特別的validator,并且不希望使用common-validator框架來定義時才像第四步那樣使用BaseController的addValidator函數加入新的validator。

          2.Binder
               一般由ServletRequestDataBinder完成Bind的工作。與其他框架自動綁定FormBean不同,Spring里需要手工調用Binder.

               但因為日期格式不固定, Binder并沒有預先包含Date的Propertity Editor。 另外數字類的Editor默認不允許字符串為空,這些都需要初始化設置。

               在Multi-action體系中,有initBinder()的callBack函數:

                  SimpleDateFormat dateFormat = new SimpleDateFormat(DateUtil.getDatePattern());
                  binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
                  binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, true));
                  binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, true));
             createBinder的另一個callBack函數是getCommandName(),getCommandName將用于在頁面用<spring:bind>綁定錯誤信息時的標識,baseController默認定為首字母小寫的類名。

           

          3.Validator
               Validator的客戶端和服務器端方案用common validator和spring moudles里的集成 。

           

          4.Bind and Validate出錯處理
              Bind and Validate出錯,一般會重新跳回輸入頁面,在頁頭以如下代碼顯示錯誤信息,并重新綁定所有數據:

          <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>


          <c:iftest="${book!=null}">
              <spring:bind path="book.*">
                  <c:if test="${not empty status.errorMessages}"><  BR>            <div class="error">
                          <c:forEach var="error" items="${status.errorMessages}">
                              ${error}<br/>
                          </c:forEach>
                      </div>
                  </c:if>
              </spring:bind>
          </c:if>

           

          posted on 2009-01-09 23:05 周銳 閱讀(450) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 修水县| 马关县| 廉江市| 西城区| 萍乡市| 东阳市| 台中县| 宜都市| 塘沽区| 会理县| 离岛区| 龙岩市| 龙南县| 鄂州市| 桓台县| 仪陇县| 吐鲁番市| 嵊泗县| 宝山区| 鄄城县| 谢通门县| 会理县| 诸城市| 屏东县| 西畴县| 扶沟县| 无为县| 沙田区| 安远县| 阿克| 霞浦县| 元朗区| 仁化县| 唐海县| 尤溪县| 青海省| 沙雅县| 恭城| 大港区| 农安县| 桐梓县|