如何在自定义Webflow Validator类中连接Spring ValidationMessages捆绑包? 我有一个验证器实现并工作:
public void validateBusinessReferences(BusinessReferencesViewDao businessReferences, Errors errors) {
if (somecondition())) {
errors.rejectValue("name", "validation.message123", "This field is bad.");
}
}
但是,我得到的不是来自 ValidationMessages.properties 文件的消息,而是回退默认值 This field is bad.
我所有其他消息和验证都工作正常 - 只是这个自定义验证程序/自定义消息方案失败了。 我怀疑存在某种 Spring 配置问题,但我无法隔离它。
理解是,ValidationMessages.properties
用于在使用JSR-303样式注释时自定义消息。由于您在这里没有使用它们 - 而是使用自定义验证器方法并直接调用errors.rejectValue
,因此您应该将消息放在标准messages.properties
文件中。在 webflow 中,此文件是特定于流的,并且与流定义 XML 文件位于同一文件夹中。
问题已解决 - 我错过了一个步骤。 为了使用属性包,您需要使用 MessageResolver 并像这样调用它:
MessageResolver messageResolver = new MessageBuilder().error().source(source).code("validation.message.property.here").defaultText(errorMessage).build();
messageResolver.resolveMessage(messageSource, Locale.ENGLISH);
return messageResolver;
其中,消息源是带有消息属性包的 Spring Bean,在应用程序上下文中定义:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
<value>ValidationMessages</value>
</list>
</property>
</bean>
有关消息解析器的文档在这里。