0
votes

I am changing from ResourceBundleMessageSource to ReloadableResourceBundleMessageSource.

I have changed my app-dispatcher-servlet.xml to reflect the requirements of ReloadableResourceBundleMessageSource. From <value>com.app.properties.windows</value> to <value>classpath:windows</value> But I am getting NoSuchMEssageException. I suspect it has to do with where I put my properties files or I am not linking it right.

Doc:

It follows the basic ResourceBundle rule of not specifying file extension or language codes, but can refer to any Spring resource location (instead of being restricted to classpath resources). With a "classpath:" prefix, resources can still be loaded from the classpath, but "cacheSeconds" values other than "-1" (caching forever) might not work reliably in this case.

Below is what I have....

Folder structure messages_en_US.properties

NotBlank.book.name = Enter something don't be lazy

app-dispatcher-servlet.xml

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>classpath:messages</value>
            <value>classpath:windows</value>
        </list>
    </property>
    <property name="defaultEncoding" value="UTF-8" />

</bean>

// some i18n and localization bean

Stack Trace

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.context.NoSuchMessageException: No message found under code 'Size.book.ispn' for locale 'zh_CN'. org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:979) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) root cause

org.springframework.context.NoSuchMessageException: No message found under code 'Size.book.ispn' for locale 'zh_CN'. org.springframework.context.support.AbstractMessageSource.getMessage(AbstractMessageSource.java:159) com.app.controller.spaController.pageSpa(spaController.java:54) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:483) org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858) javax.servlet.http.HttpServlet.service(HttpServlet.java:622) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

The code that executes is in the controller

public class someController(){   
@autowired
private MEssageSource messageSource;

public String pageSpa(Model model){
    System.out.println("loading index page");

    //System.out.println(((CustomMessageSource) messageSource).listMessageProperties("book", locale));
    Locale locale = LocaleContextHolder.getLocale();
    System.out.println(messageSource.getMessage("NotBlank.book.name", null, locale));

    return "spa";
}

Thank you

2
can you share the stack trace and the code that reads the message - Haim Raman
I have added stack trace - Eric Huang
I think you should put message resource files into the resource folder under WEB-INF, it should be there by default. - VPK
have you tried my answer below? - Haim Raman
@VPK the doc: but can refer to any Spring resource location (instead of being restricted to classpath resources). I would think with Reloadable.... it shouldn't matter. Unless I am miss understanding the doc. - Eric Huang

2 Answers

1
votes

What works for me is the following Move your message files under WEB-INF

Change the configurations as following

<bean id="messageSource"
  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>WEB-INF/messages</value>
            <value>WEB-INF/windows</value>
        </list>
    </property>
    <property name="defaultEncoding" value="UTF-8" />
    <property name="cacheSeconds">
        <value>1</value>
    </property>
</bean>

I tried to use classpath prefix as indicated in the documentation but it did not work

See also: Reloading of properties file which is loaded using setBundle

0
votes

What I end up doing is this

<bean id="messageSource"
    class="com.app.service.CustomMessageSource">
    <property name="basenames">
        <list>
            <value>classpath:com/app/properties/messages</value>
            <value>classpath:com/app/properties/windows</value>
        </list>
    </property>
    <property name="defaultEncoding" value="UTF-8" />

</bean>

Since classpath: contains WEB-INF/class. I just explicitly included the actual class path to my properties files.

I am not sure why following the doc doesn't work. But this worked for me. Hope this helped others from days of agony.