Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically configuration of the ResourceBundleMessageSource

Tags:

spring-mvc

I am trying to configure Spring MVC programmatically instead of xml files. Almost everything is working fine, but I am having troubles with the ResourceBundleMessageSource declaration.

My configuration class looks like this:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "xx.xx.xx.spring.controller")

public class MvcConfig {

    @Bean
    public ResourceBundleMessageSource configureResourceBundleMessageSource() {
        ResourceBundleMessageSource resource = new ResourceBundleMessageSource();
        resource.setBasename("messages");
        return resource;
    }

    @Bean
    public UrlBasedViewResolver configureUrlBasedViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(
            org.springframework.web.servlet.view.JstlView.class);
        return resolver;
    }
}

And my initializer like this:

public class Initializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) 
            throws ServletException {

        AnnotationConfigWebApplicationContext mvcContext = 
            new AnnotationConfigWebApplicationContext();
        mvcContext.register(MvcConfig.class);
        mvcContext.setServletContext(servletContext);
        mvcContext.refresh();
        ServletRegistration.Dynamic menu = servletContext.addServlet("menu", 
            new DispatcherServlet(mvcContext));
        menu.setLoadOnStartup(1);
        menu.addMapping("*.html");
    }
}

The application is working, but it is not showing the messages from messages.properties that is located in /WEB-INF/classes/messages.properties. And if I use xml files it works fine too.

In the JSP I have the following line±

<fmt:message key="heading"/>

And it is displayed like ???heading??? in the browser.

I dont know if it is a problem with the path or I need to put more params.

like image 591
Enrique Fernández-Polo Avatar asked Jan 18 '23 21:01

Enrique Fernández-Polo


1 Answers

and finally It's working! The problem was in the method name. If you want to create a bean with a id, the id of the bean is the name of the method. So i have changed the name of the method (configureResourceBundleMessageSource()) to messageSource().

like image 67
Enrique Fernández-Polo Avatar answered Jan 31 '23 09:01

Enrique Fernández-Polo