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 周銳 閱讀(460) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 莆田市| 合江县| 五华县| 织金县| 无棣县| 万山特区| 七台河市| 墨竹工卡县| 广宗县| 云林县| 乐安县| 贡嘎县| 深水埗区| 克东县| 宜川县| 唐海县| 沁源县| 上蔡县| 岱山县| 安岳县| 台中市| 兴海县| 醴陵市| 邮箱| 德令哈市| 桂东县| 介休市| 敦化市| 平遥县| 揭阳市| 大宁县| 来凤县| 勃利县| 呼图壁县| 漳州市| 长汀县| 平乡县| 长乐市| 临城县| 凉城县| 凌海市|