Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caused by: org.springframework.context.NoSuchMessageException: No message found under code

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

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.

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

Edit If I add the bean definition to applicationContext instead of spring-servlet it works .. ?

like image 833
NimChimpsky Avatar asked Jun 20 '12 10:06

NimChimpsky


2 Answers

I'll go for my try:

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

<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

Edit - Final try !

What 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>
like image 135
Olivier Coilland Avatar answered Nov 15 '22 17:11

Olivier Coilland


Keep your message property files outside the classpath(WEB-INF/classes) and define the bean as below

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

like image 32
Sunil Chavan Avatar answered Nov 15 '22 16:11

Sunil Chavan