嘗試本地化靜態字符串時,消息顯示時被問號「??」包圍,Thymeleaf和Spring Boot不從本地化文件讀取屬性 - 出現問號而不是本地化字符串
例如?? ticket.type_en_US ??
<p th:text="#{ticket.type}">Blah</p>
- 我使用SpringBoot 1.3.6.RELEASE
- Thymeleaf:3.0.0.RELEASE
- thymeleaf-spring4神器
我已經配置我的消息的基名在application.properties中
以及該messages.properties和messages_en_US.prope的內容rties是:
ticket.type=BUGS!!!!
配置:
spring.messages.basename=messages
在啓動時輸出:
2016年7月19日08:38:28.673 DEBUG 5175 --- [主] ationConfigEmbeddedWebApplicationContext:使用MessageSource [org.springframework.context.support.ResourceBundleMes sageSource: basenames = [messages]]
我也嘗試在下面的代碼中使用MessageResource進行編程。我將messages.properties文件放在與application.properties文件相同的文件夾中。 SRC /主/資源/
@Configuration
@ComponentScan("controller")
public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware{
private ApplicationContext applicationContext;
@Autowired
private MessageSource messageSource;
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
@Bean
public TemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
engine.setMessageSource(messageSource);
return engine;
}
@Bean
public ITemplateResolver templateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/WEB-INF/templates/");
resolver.setTemplateMode(TemplateMode.HTML);
return resolver;
}
}
爲了完整這裏是我的應用程序配置(與其他人一樣,我不得不exluse的Thymeleaf類):
@SpringBootApplication(exclude={org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration.class})
public class Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}
}
我也驗證了消息綁定的通過普林加載了內容上我的REST端點調用之一:
@Autowired
private MessageSource messageSource;
@GET
@Produces("application/json")
public List<MyData> getData() {
System.out.println("HERE 1 in Conversions");
System.out.println(messageSource.getMessage("ticket.type", null, Locale.US));
return getTheData();
}
這打印出以下,所以我知道春天引導我■裝載資源包,但Thymeleaf不接他們莫名其妙:
BUGS!!!!
這裏是我完整的HTML網頁,也許有一個與它的問題:
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Kitchen Sink</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.4/css/bootstrap.min.css"
th:href="@{/webjars/bootstrap/3.3.4/css/bootstrap.min.css}"
rel="stylesheet" media="screen" />
<script src="http://cdn.jsdelivr.net/webjars/jquery/2.1.4/jquery.min.js"
th:src="@{/webjars/jquery/2.1.4/jquery.min.js}"></script>
<link href="../static/css/mike.css"
th:href="@{css/mike.css}" rel="stylesheet" media="screen"/>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>Hello</h1>
<h2>Welcome to the Kitchen Sink!</h2>
<p th:text="#{ticket.type}">Blah</p>
<p th:text="#{test.type}">dssfgf</p>
</div>
</body>
</html>
這種類似http://stackoverflow.com/questions/28750292/custom-spring-boot-starter-how-do -you-contribute-i18n-messages-to-the-messageso – Saravana
你試過'ReloadableResourceBundleMessageSource'而不是'ResourceBundleMessageSource'嗎? – Lucky
你爲什麼要自己配置它? Spring Boot已經爲您配置了百里香葉,本地化等......使用框架而不是圍繞框架。 –