关键词:
【中文标题】Thymeleaf + Spring (not Boot) - 如何显示来自 messageSource 的消息【英文标题】:Thymeleaf + Spring (not Boot) - how to show messages from messageSource 【发布时间】:2016-08-13 15:43:19 【问题描述】:我在使用 Thymeleaf 设置 Spring MVC(不使用 Boot,因为我在发现 Spring Initializr 之前启动它)以显示来自我的资源包的消息时遇到问题。
应用的主要配置类是:
@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
应用程序的总体视图是:
messages_pt_BR.properties 文件是:
#messages
spring.messages.basename=i18n/messages
app.name=APPLICATION NAME
但是,当我尝试在页面上的某处显示 APPLICATION NAME
时,使用以下代码:
<p th:text="#app.name">app.name</p>
我只得到
??app.name_pt_BR??
我浏览了许多可能的解决方案和教程,包括:
I18n in Spring boot + Thymeleaf http://www.concretepage.com/spring-4/spring-4-mvc-internationalization-i18n-and-localization-l10n-annotation-example How to display messages in Thymeleaf and Spring Boot?
我还是不能让它工作。
浏览器和操作系统设置为语言环境 en_US。
如何解决?
【问题讨论】:
检查src/main/resources/messages_pt_br.properties中的文件是否有相同的内容。 Spring取这个特定文件夹中的一个 Hassam,感谢您的意见,但我仍然没有运气。 还在坚持吗? 是的!仍然没有运气...... 什么版本的 Spring 和 Thymeleaf? 【参考方案1】:我在这个问题上找到了我的问题的答案:
Spring 4 with thymeleaf Internationalization not identify message from resource properties file
基本上,解决问题的方法是将以下@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,例如:
@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;
从那以后,我得到了它按预期工作。
感谢您的见解和帮助!
【讨论】:
【参考方案2】:在这里使用 Spring Boot,但我能够重现您的问题,并且一旦我将其更改为 ResourceBundleMessageSource
,它就与 ReloadableResourceBundleMessageSource
相关联,它就可以正常工作。
@Bean(name = "messageSource")
public MessageSource messageSource()
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("/i18n/messages");
messageSource.setFallbackToSystemLocale(false);
messageSource.setCacheSeconds(0);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
更新:
发现您需要为 ReloadableReourceBundleMessageSource 提供完整路径,因此如果您的 /il8n
目录在您的类路径中,您需要将您的基本名称设置为 classpath:/il8n/messages
在这里查看我的帖子:Why does Thymeleaf International only works with ResourceBundleMessageSource
【讨论】:
@gtludwig 修改了发现可行的答案。【参考方案3】: @Bean(name="localeResolver")
public LocaleResolver localeResolver()
SessionLocaleResolver resolver = new SessionLocaleResolver();
resolver.resolver.setDefaultLocale(new Locale("en"));
return resolver;
更改了您的 localresolver bean,这可能会对您有所帮助。如果您将浏览器设置为en locale
,应用程序如何读取pt_BR
,将其更改为味精属性文件中的_en
或者您可以将浏览器区域设置更改为Portuguese - BRAZIL (BR) (pt_BR)
,而无需在任何地方更改代码
【讨论】:
您是否尝试过将浏览器区域设置更改为葡萄牙语-巴西?不改变任何东西在 Spring 中使用 Thymeleaf 代替 JSP 有啥好处?
】在Spring中使用Thymeleaf代替JSP有啥好处?【英文标题】:WhatistheadvantageofusingThymeleafinsteadofJSPinSpring?在Spring中使用Thymeleaf代替JSP有什么好处?【发布时间】:2018-02-0611:42:07【问题描述】:我想知道在Spring中使用Thymeleaf而不是JSP可... 查看详情
Spring - Thymeleaf:异常处理模板
】Spring-Thymeleaf:异常处理模板【英文标题】:Spring-Thymeleaf:Exceptionprocessingtemplate【发布时间】:2019-10-1121:10:57【问题描述】:我正在关注SpringinAction第5版一书,但我认为这是一个错误。这是本书的GitHub。我已经到达第3章tacos-jdbcs... 查看详情
Spring,Spring Security 和 Thymeleaf,用户是不是登录
】Spring,SpringSecurity和Thymeleaf,用户是不是登录【英文标题】:Spring,SpringSecurityandThymeleaf,IsuserLoggedInOrNotSpring,SpringSecurity和Thymeleaf,用户是否登录【发布时间】:2020-08-2910:56:59【问题描述】:我正在实现一个SpringMVCWeb应用程序。... 查看详情
Spring+Thymeleaf:从模态表单发布数据
】Spring+Thymeleaf:从模态表单发布数据【英文标题】:Spring+Thymeleaf:postdatafrommodalform【发布时间】:2018-05-1702:25:14【问题描述】:我需要将数据从html表单发布到数据库中。我有springboot和thymeleaf的gradle项目。我有java对象类女巫有id... 查看详情
sec:authorize 不起作用 - Spring Boot 2、Thymeleaf 3、Thymeleaf Spring Security 5 集成包
】sec:authorize不起作用-SpringBoot2、Thymeleaf3、ThymeleafSpringSecurity5集成包【英文标题】:sec:authorizedoesnotwork-SpringBoot2,Thymeleaf3,ThymeleafSpringSecurity5integrationpackage【发布时间】:2020-08-0508:42:22【问题描述】:我正在合作SpringBoot2.2.5百里 查看详情
thymeleaf(代码片段)
Thymeleaf一、入门启动流程:添加pom依赖-添加控制器-html添加头文件-编辑properties文件pom依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>控制器html修改声... 查看详情
Thymeleaf-Spring4 无法自动装配 TemplateEngine
】Thymeleaf-Spring4无法自动装配TemplateEngine【英文标题】:Thymeleaf-Spring4unabletoautowireTemplateEngine【发布时间】:2014-07-2113:29:58【问题描述】:我正在尝试将Thymeleaf电子邮件模板添加到工作中的SpringMVC+Thymeleaf应用程序中,如here所述。... 查看详情
Spring Boot 和 Thymeleaf:链接列表
】SpringBoot和Thymeleaf:链接列表【英文标题】:SpringBoot&Thymeleaf:listoflinks【发布时间】:2020-11-2621:21:05【问题描述】:此处使用Thymeleaf作为模板引擎的Java/SpringBootWeb应用程序。我的豆子:publicclassInventoryItemprivateStringmodelNumber;priv... 查看详情
CSS 样式表不适用于 Spring Security + Spring Boot + Thymeleaf
】CSS样式表不适用于SpringSecurity+SpringBoot+Thymeleaf【英文标题】:CSSstylesheetdoesn\'tworkwithSpringSecurity+SpringBoot+Thymeleaf【发布时间】:2021-01-1714:51:46【问题描述】:经过大量研究,我无法找到解决方案。我有一个使用SpringBoot+SpringSecurit... 查看详情
使用 spring、thymeleaf 和 mongo 填充 html 选择
】使用spring、thymeleaf和mongo填充html选择【英文标题】:Populateanhtmlselectwithspring,thymeleaf,andmongo【发布时间】:2017-03-2421:12:04【问题描述】:我正在尝试使用Spring和Thymeleaf使用Mongo集合中的数据填充HTML选择。我已经尝试遵循这些问... 查看详情
Spring + Thymeleaf 绑定表单 NullpointerException
】Spring+Thymeleaf绑定表单NullpointerException【英文标题】:Spring+ThymeleafbindingformNullpointerException【发布时间】:2015-02-0103:15:53【问题描述】:我目前正在处理用户编辑表单。我有一个具有以下结构的用户对象(伪代码):UserStringname,i... 查看详情
Thymeleaf + spring boot 不能一起工作
】Thymeleaf+springboot不能一起工作【英文标题】:Thymeleaf+springbootnotworkingtogether【发布时间】:2017-02-2808:02:47【问题描述】:我想将springboot与thymeleaf一起使用。我创建了一个控制器,如下所示:我有这样的项目结构:packagecom.mbtimeet... 查看详情
使用 Thymeleaf 从 Spring 模型设置 JavaScript 变量
】使用Thymeleaf从Spring模型设置JavaScript变量【英文标题】:SettingupaJavaScriptvariablefromSpringmodelbyusingThymeleaf【发布时间】:2014-10-3012:31:09【问题描述】:我使用Thymeleaf作为模板引擎。如何将变量从Spring模型传递给JavaScript变量?弹簧... 查看详情
datetimepicker 值未绑定到带有 thymeleaf 的属性 spring
】datetimepicker值未绑定到带有thymeleaf的属性spring【英文标题】:datetimepickervalueisnotbindingtothepropertyspringwiththymeleaf【发布时间】:2018-02-1414:37:37【问题描述】:我正在使用thymeleaf、Spring和jQuery。我已将spring中的模型对象设置为eventCr... 查看详情
Thymeleaf 3 URL 解析不适用于 Spring Boot 4 + Spring Security
】Thymeleaf3URL解析不适用于SpringBoot4+SpringSecurity【英文标题】:Thymeleaf3URLresolvingnotworkingwithSpringBoot4+SpringSecurity【发布时间】:2017-06-0905:08:02【问题描述】:在SpringBoot4中使用Gradle配置配置Thymeleaf3之后compile(\'org.springframework.boot 查看详情
Thymeleaf 和 Spring 引导以及 Spring 安全性不起作用
】Thymeleaf和Spring引导以及Spring安全性不起作用【英文标题】:ThymeleafandSpringbootandSpringsecuritynotworking【发布时间】:2019-07-2714:14:44【问题描述】:我正在尝试将thymeleaf、springsecurity和springboot一起运行,但我不确定如何将它们集成... 查看详情
使用 Spring + Thymeleaf 时出现 java.lang.***Error
】使用Spring+Thymeleaf时出现java.lang.***Error【英文标题】:Gettingjava.lang.***ErrorwhileusingSpring+Thymeleaf【发布时间】:2020-06-1703:25:33【问题描述】:我正在使用JavaSpringBoot+Thymeleaf开发一个分解系统。我收到此错误:>WhitelabelErrorPage>Th... 查看详情
thymeleaf-spring4:jar 找不到工件 - gradle intellij
】thymeleaf-spring4:jar找不到工件-gradleintellij【英文标题】:thymeleaf-spring4:jarcouldnotfindartifact-gradleintelijj【发布时间】:2018-11-2600:36:31【问题描述】:我正在尝试使用gradle、groovy和spring-boot框架和以下代码构建一个简单的应用程序:@G... 查看详情