Java Thymeleaf和Spring Boot未从本地化文件读取属性-出现问号而不是本地化字符串
当试图本地化静态字符串时,将显示消息,消息周围有问号“?” e、 g.?票证。键入Java Thymeleaf和Spring Boot未从本地化文件读取属性-出现问号而不是本地化字符串,java,spring,spring-boot,localization,thymeleaf,Java,Spring,Spring Boot,Localization,Thymeleaf,当试图本地化静态字符串时,将显示消息,消息周围有问号“?” e、 g.?票证。键入 <p th:text="#{ticket.type}">Blah</p> 配置: spring.messages.basename=messages @SpringBootApplication(exclude={org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration.class}) pub
<p th:text="#{ticket.type}">Blah</p>
配置:
spring.messages.basename=messages
@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);
}
}
启动时输出:
spring.messages.basename=messages
@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);
}
}
2016-07-19 08:38:28.673调试5175---[main]
ationConfigEmbeddedWebApplicationContext:使用MessageSource
[org.springframework.context.support.ResourceBundleMessageSource:
basenames=[消息]]
我还尝试通过编程在下面的代码中使用MessageResource。我将messages.properties文件与application.properties文件放在同一文件夹中。src/main/resources/
@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;
}
}
为了完整起见,这里是我的应用程序配置(与其他配置一样,我必须排除Thymeleaf类):
spring.messages.basename=messages
@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();
}
这是我的完整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>
厨房水槽
你好
欢迎来到厨房水槽!
诸如此类
dssfgf
好的,我想出来了。谢谢@M.Deinum指出我应该让spring boot和Thymeleaf做他们应该做的事
我必须设定:
engine.setMessageSource(messageSource);
并加入:
@Bean
添加到其他两个功能。这允许将MessageSource传递到引擎中并正确解析属性
我将用正确的来源更新上述问题,以便人们可以将其用于参考请添加
@启用自动配置
在您的Spring boot启动中,它看起来像
@EnableAutoConfiguration
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}
}
我有一个类似的问题,你的帖子中的代码对我不起作用。我的问题的根源是,我没有message.properties(没有任何语言) 我有:
messages_en.properties
messages_de.properties
messages_es.properties
但它不起作用
只有当我加上
messages.properties
与此类似,您是否尝试了
reloadableSourceBundleMessageSource
而不是ResourceBundleMessageSource
?为什么要自己配置它?Spring Boot已为您配置百里香叶、本地化等。。。使用框架而不是围绕框架工作。@Saravana我也试图修改我的代码并使用“classpath:messages”,但它没有死掉work@M.Deinum是的,这就是我想做的。我是这样开始的,但在看了这个示例之后,我尝试以编程方式对其进行配置。我删除了这4个函数,它在启动时匹配。MessageSourceAutoConfiguration matched-找到spring.messages.basename的捆绑包:消息(MessageSourceAutoConfiguration.ResourceBundleCondition)-@ConditionalOnMissingBean(类型:org.springframework.context.MessageSource;SearchStrategy:current)未找到任何bean(OnBeanCdition)