以 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 的所有校验配置。
实际上有一个重大的设计缺陷