最近在學習thymeleaf模板引擎,在使用th:text讀取國際化文本時讀取不到值,如下:
資源目錄結構如下:
index.html內容:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="#{home.welcome}"></p>
</body>
</html>
home.properties內容:
home.welcome=你好,thymeleaf!
在度娘的幫助下,找到了問題的所在,spring.messages.basename默認值為messages,根據properties的位置修改為:
spring:
messages:
basename: msg/home
再次訪問,值倒是能讀取出來了,只是中文變成亂碼了,結果如下,出現了編碼問題:
在確認index.html的編碼和spring.messages.encoding的值都為utf-8后,突然想到會不會是home.properties本身編碼不對呢,馬上打開idea的編碼設置界面,果然,properties文件默認的編碼為GBK:
修改默認編碼為UTF-8后,再修改home.properties中變成亂碼的內容,再次訪問問題解決了:
我的博客即將同步至騰訊雲+社區,邀請大家一同入駐:https://cloud.tencent.com/developer/support-plan?invite_code=36pt0wzn7g4kw