Spring MVC自定义错误和国际化

Chr*_*kis 1 validation spring-mvc internationalization custom-errors

在我的Web应用程序中,我使用注释处理错误.一切正常,我可以通过"message"参数使用自定义消息.

@Digits(fraction = 0, integer = 3, message="my custom error message...")
private String price;
Run Code Online (Sandbox Code Playgroud)

现在,我正在尝试使用.properties文件将此消息国际化,但我肯定会错过一些内容而无法使其工作.

我的春天配置:

<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <beans:property name="basenames" value="classpath:i18n/messages, classpath:i18n/errors" />
    <beans:property name="defaultEncoding" value="UTF-8" />
</beans:bean>

<beans:bean name="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <beans:property name="validationMessageSource">
        <beans:ref bean="messageSource" />
    </beans:property>
</beans:bean>

<beans:bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver">
    <beans:property name="defaultLocale" value="fr" />
</beans:bean>
Run Code Online (Sandbox Code Playgroud)

我的新豆子:

@Digits(fraction = 0, integer = 3)
private String price;
Run Code Online (Sandbox Code Playgroud)

我的"errors_fr.properties"文件.我已经尝试了一切:

Digits.myBean.myNestedBean.price = my custom error message...
Digits.myNestedBean.price = my custom error message...
javax.validation.constraints.Digits.myNestedBean.price = my custom error message...
Run Code Online (Sandbox Code Playgroud)

我总是从spring获得相同的通用消息,就像spring没有检测到我的.properties文件一样.顺便说一下,调试时可以在BindingResult对象中找到上面的消息键.

我在这里错过了什么?

请注意,我已经在我的jsp(在"messages_fr.properties"文件中)中有国际化的消息,它们工作正常.

vin*_*tks 5

我的申请中遇到了类似的问题,我希望这可以帮到你.

正如本主题http://forum.springsource.org/showthread.php?73240-Roo-JSR-303-Validations-and-Localization所述,您需要:

  1. 定义文件ValidationMessages.properties中注释引用的错误消息
  2. 在您的注释中,请参阅大括号中的错误消息键:

@Digits(fraction = 0, integer = 3, message="{message.key}")

希望这可以帮助.