• srping boot thymeleaf 学习总结 (2)


    thymeleaf获取配置properties中的数据与thymeleaf国际化(摘录)

    使用thymeleaf提供的国际化

    有时候会有直接在模板中获取配置文件properties中的配置信息,比如:webname = form1,不需要用java传给模板,在模板中就可以直接获取的方法

    首先我们先定义国际化资源文件,spring boot默认就支持国际化的,而且不需要你过多的做什么配置,

    只需要在resources/下定义国际化配置文件即可,注意名称必须中messages开始要不然识别不了

    因为springboot默认将国际化的配置文名称定义为messages,当然你也可以改变这个默认的配置

    我们定义如下几个文件:

    messages.properties (默认,当找不到语言的配置的时候,使用该文件进行展示)。
    messages_zh_CN.properties(中文)
    messages_en_US.properties(英文) 

    具体的代码如下:

    messages.properties:
    welcome = 欢迎你登录form1.cn网站(default
     
    messages_zh_CN.properties:
    welcome = u6b22u8fceu4f60u767bu5f55u5230 u963fu91ccu5df4u5df4 u7f51u7ad9uff08u4e2du6587uff09 #unicode
     
    messages_en_US.properties:
    welcome = welcome to login to form.cn website(English)

    修改hello.html文件,使用#{key}的方式进行使用messages中的字段信息: 

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8" />
        <title>hello spring boot</title>
    </head>
    <body>
        <p><label th:text="#{welcome}"></label></p>
    </body>
    </html>


    重新访问:http://127.0.0.1:8080/hello 应该显示: 

    欢迎你登录到form1.cn网站(中文)

    如果网站没有多语言的需求,那么就做一个 messages.properties 来放自己的一些配置

    注:如果你的messages.properties中文乱码,那么 对工程进行 settings 找到 File Encodings 然后改为utf-8即可

    原文连接 :https://www.form1.cn/java-java-188.html

  • 相关阅读:
    spring mvc标准项目结构
    Logger之Logger.getLogger(CLass)
    java的占位符
    java 代码格式(转)
    解决cookie跨域访问
    web页面防盗链功能使用--request.getHeader("Referer")
    【Properties】Properties的load方法
    Servlet生命周期与工作原理
    面试中关于Java你所需知道的的一切
    数据库索引的实现原理
  • 原文地址:https://www.cnblogs.com/newnj/p/9111489.html
Copyright © 2020-2023  润新知