hk2000c技術專欄

          技術源于哲學,哲學來源于生活 關心生活,關注健康,關心他人

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            111 隨筆 :: 1 文章 :: 28 評論 :: 0 Trackbacks

          以 BaseCommandController 為例
          protected void initApplicationContext() {
            if (this.validators != null) {
             for (int i = 0; i < this.validators.length; i++) {
              if (this.commandClass != null && !this.validators[i].supports(this.commandClass))
               throw new IllegalArgumentException("Validator [" + this.validators[i] +
                 "] does not support command class [" +
                 this.commandClass.getName() + "]");
             }
            }
           }

          子類配置如下

           <bean id="addNewsController" class="AddNewsController" scope="request">
                  <property name="formView" value="/management/news/addNews"/>
                  <property name="validator" ref="beanValidator"/>
                  <property name="successView" value="forward:/management/news/newsList.html"/>
                  <property name="commandClass" value="News"/>
                  <property name="commandName" value="news"/>
              </bean>

          public final void setValidator(Validator validator) {
            this.validators = new Validator[] {validator};
           }

          設置Validator數組

          在初始化的時候,檢驗 是否支持command 類型
          this.validators[i].supports(this.commandClass)

          此support 為 org.springframework.validation.Validator 的所有 實現類的 方法,檢驗支持檢驗類的動作。


          舉例 配置
           <bean id="beanValidator" class="org.springmodules.validation.commons.DefaultBeanValidator">
                  <property name="validatorFactory" ref="validatorFactory"/>
              </bean>

          DefaultBeanValidator extends AbstractBeanValidator implements Validator

          再看實現方法

             /**
               * Checks if the validatorFactory is configured to handle this class.  Will
               * convert the class into a form name, suitable for commons validator.
               *
               * @return <code>true</code> if the validatorFactory supports the class,
               *         or <code>false</code> if not
               * @see #getFormName(Class)
               */
              public boolean supports(Class clazz) {
                  boolean canSupport = validatorFactory.hasRulesForBean(getFormName(clazz), getLocale());
                  if (log.isDebugEnabled()) {
                      log.debug("validatorFactory " + (canSupport ? "does" : "does not")
                          + " support class " + clazz + " with form name " + getFormName(clazz));
                  }
                  return canSupport;
              }

          檢驗是否支持輸入類

          另一個方法
           /**
               * If <code>useFullyQualifiedClassName</code> is false (default value), this function returns a
               * string containing the uncapitalized, short name for the given class
               * (e.g. myBean for the class com.domain.test.MyBean). Otherwise, it  returns the value
               * returned by <code>Class.getName()</code>.
               *
               * @param cls <code>Class</code> of the bean to be validated.
               * @return the bean name.
               */
              protected String getFormName(Class cls) {
                  return (this.useFullyQualifiedClassName) ? cls.getName() : Introspector.decapitalize(ClassUtils.getShortName(cls));
              }
           Introspector.decapitalize(ClassUtils.getShortName(cls) 獲得按照西班牙命名法的form 名

          這個方法本意是獲得以類名為formName 的所有校驗配置。

          實際上有一個重大的設計缺陷






          posted on 2007-10-26 00:48 hk2000c 閱讀(1848) 評論(0)  編輯  收藏 所屬分類: CMS項目開發
          主站蜘蛛池模板: 离岛区| 雷山县| 隆尧县| 阜宁县| 化州市| 武功县| 玉林市| 永寿县| 道真| 衡东县| 内江市| 克什克腾旗| 江都市| 广南县| 静安区| 嘉黎县| 武清区| 北流市| 清苑县| 高安市| 长垣县| 石泉县| 肇州县| 霍邱县| 金沙县| 赞皇县| 呼和浩特市| 陆良县| 大关县| 哈巴河县| 南安市| 福安市| 吉安县| 东城区| 屏东市| 北海市| 阿荣旗| 华亭县| 池州市| 洛浦县| 莲花县|