【发布时间】:2014-10-06 19:39:14
【问题描述】:
我正在使用 Spring 4.0.7 和 Spring Web Flow 2.4.0
我有以下(我正在使用JSR 349):
对于实体:
@Min(value=1, message="{person.age.min}",groups={PersonRegistrationCheck.class})
@Max(value=115, message="{person.age.max}",groups={PersonRegistrationCheck.class})
@NotNull(message="{field.null}",groups={PersonRegistrationCheck.class})
@Column(name="age", nullable=false, length=3)
@XmlElement
public Integer getAge() {
return age;
}
在ValidationMessages.properties 文件中
person.age.max = Invalid data '${validatedValue}', the maximum value allowed is {value}
person.age.min = Invalid data '${validatedValue}', the minimum value allowed is {value}
在一些@Configuration
@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean(ReloadableResourceBundleMessageSource messageSource){
LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
localValidatorFactoryBean.setValidationMessageSource(messageSource);
MessageSourceResourceBundleLocator msrbl = new MessageSourceResourceBundleLocator(messageSource);
ResourceBundleMessageInterpolator rbmi = new ResourceBundleMessageInterpolator(msrbl);
localValidatorFactoryBean.setMessageInterpolator(rbmi);
return localValidatorFactoryBean;
}
通过 Spring MVC,它工作得非常好:
<tr>
<td><spring:message code="person.form.age"/></td>
<td><form:input path="age" size="40"/></td>
<td><form:errors path="age" cssClass="error"/></td>
</tr>
如果我输入一个无效值,我将如何得到 500
无效数据500,允许的最大值为115
我可以看到无效数据和允许的最大值。
直到这里一切都好..
注意:对于Spring MVC,不需要进行关于LocalValidatorFactoryBean的特殊注入
问题在于 Spring Web Flow。
注意:有必要为Spring Web Flow做一个关于LocalValidatorFactoryBean的特殊注入
这里是它的配置:
@Autowired
private LocalValidatorFactoryBean localValidatorFactoryBean;
@Bean
public FlowBuilderServices flowBuilderServices() {
return getFlowBuilderServicesBuilder()
.setViewFactoryCreator(mvcViewFactoryCreator())
.setConversionService(conversionService())
.setValidator(localValidatorFactoryBean)
.setDevelopmentMode(true)
.build();
}
以上实际上几乎与spring-webflow-samples / WebFlowConfig相同
在流定义中
<view-state id="start"
view="person.flow.form.register"
model="person"
validation-hints="'com.manuel.jordan...PersonRegistrationCheck'" >
<transition on="submit" to="address" bind="true" validate="true" >
<evaluate expression="personAction.savePerson(flowRequestContext)" />
</transition>
<transition on="cancel" to="end" bind="false" validate="false"/>
</view-state>
再次在 .jsp 文件中
<tr>
<td><spring:message code="person.form.age"/></td>
<td><form:input path="age" size="40"/></td>
<td><form:errors path="age" cssClass="error"/></td>
</tr>
注意:它是其他或新的 .jsp 文件,它适用于 Spring Web Flow。
因此,一个通过Spring MVC 工作,另一个通过SWF 工作。
用于测试目的
好的问题:
再次,如果我输入一个无效值,我将如何得到 500
无效数据$validatedValue,允许的最大值为value
观察:我们看不到,无效数据(保留在 $validatedValue 中)和 value 仍然是静态的。
因此:
-
Spring MVC显示:Invalid data 500, the maximum value allowed is 115 -
SWF显示:Invalid data $validatedValue, the maximum value allowed is value
注意:即使使用下面的,一切都会出错。
@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean(ReloadableResourceBundleMessageSource messageSource){
LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
localValidatorFactoryBean.setValidationMessageSource(messageSource);
//MessageSourceResourceBundleLocator msrbl = new MessageSourceResourceBundleLocator(messageSource);
//ResourceBundleMessageInterpolator rbmi = new ResourceBundleMessageInterpolator(msrbl);
//localValidatorFactoryBean.setMessageInterpolator(rbmi);
return localValidatorFactoryBean;
}
出了什么问题或遗漏了什么?
【问题讨论】:
标签: spring-mvc spring-webflow spring-3 spring-webflow-2 spring-4