Thymeleaf + Spring(不启动) - 如何显示来自messageSource的消息 [英] Thymeleaf + Spring (not Boot) - how to show messages from messageSource
问题描述
我在使用Thymeleaf设置我的Spring MVC(不使用Boot,因为我在发现Spring Initializr之前启动它)时,无法显示来自我的资源包的消息。
I'm having trouble setting up my Spring MVC (not using Boot, because I started it prior to discovering Spring Initializr) with Thymeleaf to show messages from my resource bundle.
该应用程序的主要配置类是:
Main configuration class for the app is:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "b.c.m.h")
public class HrmConfiguration extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setPrefix("/WEB-INF/html/");
templateResolver.setSuffix(".html");
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addDialect(new SpringSecurityDialect());
engine.addDialect(new LayoutDialect(new GroupingStrategy()));
engine.setTemplateResolver(templateResolver);
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(engine);
viewResolver.setCache(false);
return viewResolver;
}
@Bean(name = "messageSource")
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("/i18n/messages");
messageSource.setFallbackToSystemLocale(false);
messageSource.setCacheSeconds(0);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
@Bean(name="localeResolver")
public LocaleResolver localeResolver(){
SessionLocaleResolver resolver = new SessionLocaleResolver();
resolver.setDefaultLocale(new Locale("pt", "BR"));
return resolver;
}
@Bean
public UrlTemplateResolver urlTemplateResolver() {
return new UrlTemplateResolver();
}
// other beans and configs
}
该应用程序的一般视图是:
The general view of the application is:
且messages_pt_BR.properties文件为:
And the messages_pt_BR.properties file is:
#messages
spring.messages.basename=i18n/messages
app.name=APPLICATION NAME
但是,当我试图展示时页面上某处的 APPLICATION NAME
,其中包含:
But, when I try to show the APPLICATION NAME
somewhere on the page, with this:
<p th:text="#{app.name}">app.name</p>
我只得到
??app.name_pt_BR??
我浏览了许多可能的解决方案和教程,包括:
I have browsed a number of possible solutions and tutorials, including:
I18n in Spring boot + Thymeleaf
< a href =http://www.concretepage.com/spring-4/spring-4-mvc-internationalization-i18n-and-localization-l10n-annotation-example =nofollow noreferrer> http:// www .concretepage.com / spring-4 / spring-4-mvc-internationalization-i18n-and-localization-l10n-annotation-example
如何在Thymeleaf和Spring Boot中显示消息?
我仍然无法正常工作。
浏览器和操作系统设置为locale en_US。
Browser and OS are set to locale en_US.
如何获取tihs解决了吗?
How to get tihs solved?
推荐答案
我在这个问题上找到了问题的答案:
I found the answer to my problem on this question:
Spring 4 with thymeleaf Internationalization not identify from resource properties file
基本上,解决问题的方法是分离以下 @Bean
...
Basically, what solved the issue was to separate the following @Bean
...
@Bean
public ViewResolver viewResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setPrefix("/WEB-INF/html/");
templateResolver.setSuffix(".html");
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addDialect(new SpringSecurityDialect());
engine.addDialect(new LayoutDialect(new GroupingStrategy()));
engine.setTemplateResolver(templateResolver);
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(engine);
viewResolver.setCache(false);
return viewResolver;
}
...分成三个不同的bean:
... into three distinct beans like:
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setCacheable(false);
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setPrefix(HTML_VIEWS);
templateResolver.setSuffix(".html");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addDialect(new SpringSecurityDialect());
templateEngine.addDialect(new LayoutDialect(new GroupingStrategy()));
templateEngine.setTemplateResolver(templateResolver());
return templateEngine;
}
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setCache(false);
viewResolver.setOrder(1);
return viewResolver;
}
从那以后,我按照我的预期运作了。
From that, I got it working as I expected.
感谢您的见解和帮助!
这篇关于Thymeleaf + Spring(不启动) - 如何显示来自messageSource的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!