Thymeleaf读取国际化文本时出现??xxxxxx_zh_CN??问题

最近在学习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

posted @ 2020-05-19 17:33  布禾  阅读(1451)  评论(0编辑  收藏  举报