无法使用messageSource解析spring消息代码
我正在尝试在 spring 中连接一个 messageSource 以用于我的应用程序。它不起作用,给出以下错误:
org.springframework.context.NoSuchMessageException:在语言环境“en”的代码“validation_required”下找不到消息。
我的 applicationContext.xml 包含 messageSource 的 def:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages</value>
</list>
</property>
</bean>
我的消息属性文件位于:
/WEB-INF/classes/messages/messages_en_US.properties
最后,我进行的生成错误的调用是:
String message = messageSource.getMessage("validation_required", null, Locale.ENGLISH);
此时有人可以帮助我吗?
I am trying to hook up a messageSource in spring to use for my application. Its not working, gives this error:
org.springframework.context.NoSuchMessageException: No message found under code 'validation_required' for locale 'en'.
my applicationContext.xml contains this def for messageSource:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages</value>
</list>
</property>
</bean>
my messages properties file lives in:
/WEB-INF/classes/messages/messages_en_US.properties
Finally, the call i made that generates the error is:
String message = messageSource.getMessage("validation_required", null, Locale.ENGLISH);
Can anyone help me at this hour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看来你的路径不正确。
由于您的包位于 /WEB-INF/classes/messages/messages_en_US.properties 下,因此您的基本名称设置应如下所示:classpath:messages/messages(在本例中,基本名称表示路径和属性文件前缀)。
It seems like your path is not correct.
since you have your bundle under /WEB-INF/classes/messages/messages_en_US.properties, your basename setting should look like: classpath:messages/messages (basename in this case means path and properties file prefix).
问题在于您定义资源包和指定区域设置的方式(它们与资源包的 搜索顺序。将包重命名为“messages_en.properties”或使用 new Locale("en" 调用“getMessage(...)” ,“美国”)。
The problem is with the way you have defined the resource bundle and the locale you are specifying (They doesn't match with the resource bundle's search order. Either rename your bundle to "messages_en.properties" or invoke the "getMessage(...)" with new Locale("en","US"). I prefer the first option.
我使用以下 bean,它工作正常,无需指定文件路径:
虽然我使用的文件简单地称为“messages_en.properties”和“messages_es.properties”,但你明白了。
当你打电话时
你会遇到异常吗?尝试使用此文件名 messages_en.properties 或 messages_us_en.properties
I use following bean and it is working fine without specifying the path to the file:
Although the files I use are simply called "messages_en.properties" and "messages_es.properties" well you get the idea.
When you call
do you get an exception? Try to use this file name messages_en.properties or messages_us_en.properties
试试这个
查看获取字符串的注释
Try this
look at the comment for getting the string