引言: Thymeleaf是Spring Boot中使用的一种后台的模板技术,在Spring Boot中碰到了在页面上无法访问messages中信息的问题,本文将描述其分析过程以及如何解决该问题。
1. 环境介绍
Spring Boot 1.4.0, Thymeleaf的模板技术后台使用。
使用基于messages的存放信息,比如中文情况下使用messages_zh_CN.properties来存放信息。
2. 问题以及错误描述
在Thymeleaf页面中使用#{message_key}来直接从messages中读取信息。 测试中页面报出来的错误信息如下:
??menu.top.client.service.text_zh_CN??
其中使用的messages.properties定义了如下信息:
menu.top.client.service.text=\u8054\u7CFB\u5BA2\u670D
但是却无法正确的显示出来,那问题出在哪里呢?
3. 页面代码分析
页面的代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello.v.2</h1>
<p th:text="${hello}"></p>
<p th:text="#{menu.top.client.service.text}">test message property</p>
</body>
</html>
分析器中<p>中使用的th:text应该没有问题。
4. messages文件的位置
如果页面是正确的话,查看messages的靖是否正确, 指定basename的路径,经过分析其为正确的。
spring.messages.basename=i18n/messages
5. 打开thymeleaf的调试模式
spring.thymeleaf.cache=false
关闭其cache设置,可以直接使用其修改的template,不再缓存。
6. 重新启动之后, 可以正常显示
在写blog的过程中,重启之后,可以正常显示了, 问题忽然之久就消失了,难道是缓存的问题,不得而知,期待下一次碰到之后,补齐本文剩余的原因内容。