spring 引起:org.springframework.context.NoSuchMessageException:在代码下找不到消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11118090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Caused by: org.springframework.context.NoSuchMessageException: No message found under code
提问by NimChimpsky
Attempting to get Spring internationalization working. I have used classpath:messages basename, created .properties files for languages. They are being corectly copied to web-inf folder and the codes exist within properties file ...
试图让 Spring 国际化工作。我使用了 classpath:messages basename,为语言创建了 .properties 文件。它们正被正确地复制到 web-inf 文件夹中,并且代码存在于属性文件中...
Here is the ide showing everything, please help me. I have copied the set up from another project I have done that works fine. I have tried creating a load of different message files, but its not picking anything up ... pic shows web.xml, spring-servlet.xml, and directory structure.
这是显示所有内容的ide,请帮助我。我已经从我完成的另一个项目中复制了设置,该项目运行良好。我曾尝试创建不同的消息文件的负载,但它没有选择任何东西......图片显示了 web.xml、spring-servlet.xml 和目录结构。


EditIf I add the bean definition to applicationContext instead of spring-servlet it works .. ?
编辑如果我将 bean 定义添加到 applicationContext 而不是 spring-servlet 它可以工作..?
回答by Olivier Coilland
I'll go for my try:
我去试试:
If the file is located under the WEB-INF/classesdirectory, try :
如果文件位于WEB-INF/classes目录下,请尝试:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="WEB-INF/classes/messages" />
</bean>
And the name of the file should be either :
文件名应该是:
- messages.properties
- messages_en.properties
- messages_en_GB.properties
- 消息.属性
- messages_en.properties
- messages_en_GB.properties
Edit- Final try !
编辑- 最后的尝试!
What about this way of writing the configuration, I smell sthg here after your last comment:
这种写配置的方式怎么样,在你上次评论后我在这里闻到了sthg:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<mvc:interceptors>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
回答by Sunil Chavan
Keep your message property files outside the classpath(WEB-INF/classes) and define the bean as below
将您的消息属性文件保留在类路径(WEB-INF/classes)之外,并按如下方式定义 bean
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages"/>
<property name="cacheSeconds" value="1"/>
As per the doc ReloadableResourceBundleMessageSource provides you benefit of changing the messages on the fly which spring fetches through the cachSeconds. This class differs from ResourceBundleMessageSource only in specifying the resource location.
根据文档 ReloadableResourceBundleMessageSource 为您提供了即时更改 spring 通过 cachSeconds 获取的消息的好处。此类与 ResourceBundleMessageSource 的不同之处仅在于指定了资源位置。
回答by Rakesh
All the configurartion details all correct but do one thing which ever property files are configured in child configuration file it means your spring-servlet.xml configure all the properties into applicationContext.xml it means parent configuration file maximum it will works. try it and remove the configuration of property details in spring-servlet.xml...........
所有的配置细节都是正确的,但做一件事,在子配置文件中配置属性文件,这意味着你的 spring-servlet.xml 将所有属性配置到 applicationContext.xml 中,这意味着父配置文件最多可以工作。试试吧,去掉spring-servlet.xml中property details的配置.......
回答by Vishal Kumar
Though to many this sounds foolish, but the mistake my code had that we had written our own MessageSource. Which was calling Spring's MessageSource.
虽然对许多人来说这听起来很愚蠢,但我的代码有一个错误,那就是我们编写了自己的 MessageSource。这是调用 Spring 的 MessageSource。
But in the code it was like (MessageSource(MessageSource)). Hence we were doing look up over look up.
但在代码中它就像 (MessageSource(MessageSource))。因此,我们正在查找而不是查找。
Removed the extra call, and its working now.
删除了额外的电话,现在可以正常工作了。

