Spring MVC自定义错误和国际化 [英] Spring MVC custom errors and internationalization

查看:169
本文介绍了Spring MVC自定义错误和国际化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my web application, I handle errors with annotations. Everything works fine and I can use custom messages via the "message" parameter.

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

现在,我正在尝试使用.properties文件使此消息国际化,但是我确实错过了一些东西,我无法使其正常工作。

Now I'm trying to internationalize this message with a .properties files, but I certainly miss something and I can't get it to work.

我的spring配置:

My spring config :

<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>

我的新豆:

@Digits(fraction = 0, integer = 3)
private String price;

我的 errors_fr.properties文件。我已经尝试了所有方法:

My "errors_fr.properties" file. I've already tried everything :

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...

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

I always get the same generic message from spring, it's like as spring doesn't detect my .properties file. By the way, the message keys above can be found in the BindingResult object when debugging.

我在这里缺少什么?

请注意,我已经在我的jsp文件中(在 messages_fr.properties文件中)对消息进行了国际化,并且它们可以正常工作。

Notice that I already have internationalized messages in my jsp's (in the "messages_fr.properties" file) and they work fine.

推荐答案

我的应用程序中有一个类似的问题,希望对您有所帮助。

I had a similar problem in my application, and I hope that this can help you.

如该线程中所述, http://forum.springsource.org/showthread.php?73240-Roo-JSR-303-Validations-and-Localization ,您需要:

As discussed in this thread, http://forum.springsource.org/showthread.php?73240-Roo-JSR-303-Validations-and-Localization, you need to:


  1. 在注释中定义ValidationMessages.properties文件中注释所引用的错误消息

  2. ,请参见错误括在大括号中的消息密钥:

@Digits(分数= 0,整数= 3,消息= {message.key})

希望这会有所帮助。

这篇关于Spring MVC自定义错误和国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆