Spring 4与thymeleaf国际化无法从资源属性文件中识别消息
使用Spring 4 + thymeleaf在我的Webapp上工作以支持国际化。我尝试了很多方法来更改位置文件和基本名称的配置,并且仍然会收到“
label.greeting _ **” 模板结果。下面是我的代码
@Configuration@EnableSpringConfigured
@EnableWebMvc
@EnableScheduling
@ComponentScan(basePackages = {"com.categorybags.web.controller", "com.categorybags.persistence.services"})
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/res/**").addResourceLocations("/res/");
}
@Bean
public ViewResolver viewResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setCacheable(false);
templateResolver.setPrefix("/WEB-INF/jsp/");
templateResolver.setSuffix(".jsp");
templateResolver.setTemplateMode("HTML5");
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setOrder(1);
viewResolver.setTemplateEngine(templateEngine);
return viewResolver;
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(new Locale("en"));
return localeResolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("WEB-INF/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
属性文件位于WEB-INF下,其路径为src \ main \ webapp \ WEB-INF \ messages_en.properties和src
\ main \ webapp \ WEB-INF \ messages_ch.properties
回答:
我想我是能够通过仅注册重现您的问题ViewResolver
到spring,而不是属于2个其它组件SpringTemplateEngine
和ServletContextTemplateResolver
。
因此,我认为您应该尝试对配置进行一些拆分,以将其他两个组件注册到spring容器中。这是代码:
@Beanpublic ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setCacheable(false);
templateResolver.setPrefix("/WEB-INF/jsp/");
templateResolver.setSuffix(".jsp");
templateResolver.setTemplateMode("HTML5");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
return templateEngine;
}
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setOrder(1);
return viewResolver;
}
以上是 Spring 4与thymeleaf国际化无法从资源属性文件中识别消息 的全部内容, 来源链接: utcz.com/qa/405902.html