Software Configuration ResourceBundleMessageSource

I am trying to configure Spring MVC programmatically instead of xml files. Almost everything works fine, but I'm having problems declaring ResourceBundleMessageSource .

My configuration class is as follows:

 @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 works, but it does not display messages from messages.properties located in /WEB-INF/classes/messages.properties . And if I use xml files, it works fine.

In JSP, I have the following line ±

 <fmt:message key="heading"/> 

And it shows up as ???heading??? in the browser.

I don't know if this is a problem along the way or if I need to add more parameters.

+4
source share
1 answer

and finally it works! The problem was the name of the method. If you want to create a bean with an identifier, the bean identifier is the name of the method. So I changed the name of the method ( configureResourceBundleMessageSource() ) to messageSource() .

+9
source

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


All Articles