Search code examples
spring-mvcinternationalizationjstlservlet-3.0tiles2

Spring 3.1 ReloadableResourceBundleMessageSource and Apache Tiles 2.2.2


What seemed to be a simple task has turned out to be a few hours of suffering. I am building a Spring 3.1 MVC application on the JavaEE 6 and Servlet 3.0.1 api without a web.xml file. I have a WebMvcConfiguration class like this fragment:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "be.collectortools.collectorsite")
public class MvcConfig extends WebMvcConfigurationSupport {

@Bean
    public ReloadableResourceBundleMessageSource messageSourceBean() {
        String[] basenames = {"classpath:messages"};

        ReloadableResourceBundleMessageSource resourceBundle = new ReloadableResourceBundleMessageSource();
        resourceBundle.setBasenames(basenames);
        resourceBundle.setDefaultEncoding("UTF-8");
        return resourceBundle;
    }
}

I have successfully setup Apache Tiles 2.2.2 together with 2 basic controllers. Now I would like to add ResourceBundles to the working Spring/Tiles application and I can't get them to work.

After searching I found some this that might go wrong:

  • Do I use fmt:message key="application.header" or should I use spring:message code="application.header" in my JSP pages? The first ignores not found values the second throws errors.
  • I use ReloadableResourceBundleMessageSource which should be 'better' or at least newer then ResourceBundleMessageSource is this ok?
  • ReloadableResourceBundleMessageSource loads files from more locations so I have specified classpath:
  • I placed the messages.properties file in the src/main/resources folder
  • Is it still correct that, when not adding a locale to the end of a bundle's name, this is used as a (default) fallback? Either way adding the "en_US" locale doesn't help.

The error:

root cause

javax.servlet.jsp.JspTagException: No message found under code 'application.header' for locale 'en_US'.
    org.springframework.web.servlet.tags.MessageTag.doStartTagInternal(MessageTag.java:184)

also the war file is not being run inside Eclipse I deploy it manually to my local tomcat 7.0.23. This also allows me to see the deployed file structure more easily and gives me better control.

I have no clue what is I am doing wrong any help would be appreciated.


Solution

  • The MessageSource bean has to be named messageSource not messageSourceBean - if you change your @Bean to the following it should resolve the messages correctly:

    @Bean
    public ReloadableResourceBundleMessageSource messageSource() {
        String[] basenames = {"classpath:messages"};
    
        ReloadableResourceBundleMessageSource resourceBundle = new ReloadableResourceBundleMessageSource();
        resourceBundle.setBasenames(basenames);
        resourceBundle.setDefaultEncoding("UTF-8");
        return resourceBundle;
    }