Reason: org.springframework.context.NoSuchMessageException: message not found by code

Trying to get Spring internationalized. I used classpath: messages basename, created .properties files for languages. They are copied to the web-inf folder, and the codes exist in the properties file ...

Here is an ideal showing everything, please help me. I copied the setup from another project that I did, which works great. I tried to create loading of different message files, but did not collect anything ... pic shows web.xml, spring -servlet.xml and directory structure.

This shows everything, I can't see what I am missing

Edit If I add a bean definition to applicationContext instead of spring -servlet, it will work.?

+6
source share
4 answers

I will go for an attempt:

If the file is located in the WEB-INF/classes , try:

 <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="WEB-INF/classes/messages" /> </bean> 

And the file name should be:

  • messages.properties
  • messages_en.properties
  • messages_en_GB.properties

Edit - Last Attempt!

How about this way of writing the configuration, I smell sthg here after your last comment:

 <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> 
+11
source

Store the message properties files outside the class path (WEB-INF / classes) and define a bean as shown below

 <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="/WEB-INF/messages"/> <property name="cacheSeconds" value="1"/> 

According to the ReloadableResourceBundleMessageSource document, you get the ability to change messages on the fly that spring retrieves through cachSeconds. This class differs from ResourceBundleMessageSource only when specifying the location of the resource.

+3
source

All configuration data is all correct, but doing one thing that ever created property files in a child configuration file means that your spring -servlet.xml will set all properties in applicationContext.xml, this means that the maximum parent configuration file will work try and remove the property information configuration in spring -servlet.xml ...........

0
source

Although this sounds silly to many, the error in my code was that we wrote our own MessageSource. Which called Spring MessageSource.

But in code, it was like (MessageSource (MessageSource)). Therefore, we look up to look up.

Removed an additional call and now its work.

-1
source

Source: https://habr.com/ru/post/918549/


All Articles