zoukankan      html  css  js  c++  java
  • SpringBoot入门系列(五)Thymeleaf的常用标签和用法

    前面介绍了Spring Boot 中的整合Thymeleaf 。不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/1657780.html

    今天我们主要来看看 Thymeleaf 的常用标签和用法!其他详细的内容,大家可以看看Thymeleaf官方使用手册 。

    这个系列课程的完整源码,也会提供给大家。大家关注我的微信公众号(架构师精进),回复:springboot源码 获取这个系列课程的完整源码。或者点此链接直接下载完整源码

    一、基础语法

    变量表达式 ${}

    使用方法:直接使用th:xx = "${}" 获取对象属性 。例如:

    <form id="userForm">
        <input id="id" name="id" th:value="${user.id}"/>
        <input id="username" name="username" th:value="${user.username}"/>
        <input id="password" name="password" th:value="${user.password}"/>
    </form>
    
    <div th:text="hello"></div>
    
    <div th:text="${user.username}"></div>

    选择变量表达式 *{}

    使用方法:首先通过th:object 获取对象,然后使用th:xx = "*{}"获取对象属性。

    这种简写风格极为清爽,推荐大家在实际项目中使用。 例如:

    <form id="userForm" th:object="${user}">
        <input id="id" name="id" th:value="*{id}"/>
        <input id="username" name="username" th:value="*{username}"/>
        <input id="password" name="password" th:value="*{password}"/>
    </form>

    URL表达式 @{}

    使用方法:通过链接表达式@{}直接拿到应用路径,然后拼接静态资源路径。例如:

    <script th:src="@{/webjars/jquery/jquery.js}"></script>
    <link th:href="@{/webjars/bootstrap/css/bootstrap.css}" rel="stylesheet" type="text/css">

    片段表达式 ~{}

    片段表达式是Thymeleaf的特色之一,细粒度可以达到标签级别,这是JSP无法做到的。
    片段表达式拥有三种语法:

    • ~{ viewName } 表示引入完整页面
    • ~{ viewName ::selector} 表示在指定页面寻找片段 其中selector可为片段名、jquery选择器等
    • ~{ ::selector} 表示在当前页寻找

    使用方法:首先通过th:fragment定制片段 ,然后通过th:replace 填写片段路径和片段名。例如:

    <!-- /views/common/head.html-->
    <head th:fragment="static">
            <script th:src="@{/webjars/jquery/3.3.1/jquery.js}"></script>
    </head>
    
    <!-- /views/your.html -->
    <div th:replace="~{common/head::static}"></div>

    在实际使用中,我们往往使用更简洁的表达,去掉表达式外壳直接填写片段名。例如:

    <!-- your.html -->
    <div th:replace="common/head::static"></div>

    注意:使用替换路径th:replace 开头请勿添加斜杠,避免部署运行的时候出现路径报错。(因为默认拼接的路径为spring.thymeleaf.prefix = classpath:/templates/

    消息表达式

    即通常的国际化属性:#{msg} 用于获取国际化语言翻译值。例如:

     <title th:text="#{user.title}"></title>

    其它表达式

    在基础语法中,默认支持字符串连接、数学运算、布尔逻辑和三目运算等。例如:

    <input name="name" th:value="${'I am '+(user.name!=null?user.name:'NoBody')}"/>

    二、迭代循环

    想要遍历List集合很简单,配合th:each 即可快速完成迭代。例如遍历用户列表:

    <div th:each="user:${userList}">
        账号:<input th:value="${user.username}"/>
        密码:<input th:value="${user.password}"/>
    </div>

    在集合的迭代过程还可以获取状态变量,只需在变量后面指定状态变量名即可,状态变量可用于获取集合的下标/序号、总数、是否为单数/偶数行、是否为第一个/最后一个。例如:

    <div th:each="user,stat:${userList}" th:class="${stat.even}?'even':'odd'">
        下标:<input th:value="${stat.index}"/>
        序号:<input th:value="${stat.count}"/>
        账号:<input th:value="${user.username}"/>
        密码:<input th:value="${user.password}"/>
    </div>

    如果缺省状态变量名,则迭代器会默认帮我们生成以变量名开头的状态变量 xxStat, 例如:

    <div th:each="user:${userList}" th:class="${userStat.even}?'even':'odd'">
        下标:<input th:value="${userStat.index}"/>
        序号:<input th:value="${userStat.count}"/>
        账号:<input th:value="${user.username}"/>
        密码:<input th:value="${user.password}"/>
    </div>

    三、条件判断

    条件判断通常用于动态页面的初始化,例如:

    <div th:if="${userList}">
        <div>的确存在..</div>
    </div>

    如果想取反则使用unless 例如:

    <div th:unless="${userList}">
        <div>不存在..</div>
    </div>

    四、日期格式化

    使用默认的日期格式(toString方法) 并不是我们预期的格式:Mon Dec 03 23:16:50 CST 2018

    <input type="text" th:value="${user.createTime}"/>

    此时可以通过时间工具类#dates来对日期进行格式化:2018-12-03 23:16:50

    <input type="text" th:value="${#dates.format(user.createTime,'yyyy-MM-dd HH:mm:ss')}"/>

    五、内联写法

    • (1)为什么要使用内联写法?·答:因为 JS无法获取服务端返回的变量。

    • (2)如何使用内联表达式?答:标准格式为:[[${xx}]] ,可以读取服务端变量,也可以调用内置对象的方法。例如获取用户变量和应用路径:

          <script th:inline="javascript">
              var user = [[${user}]];`
              var APP_PATH = [[${#request.getContextPath()}]];
              var LANG_COUNTRY = [[${#locale.getLanguage()+'_'+#locale.getCountry()}]];
          </script>
    • (3)标签引入的JS里面能使用内联表达式吗?答:不能!内联表达式仅在页面生效,因为Thymeleaf只负责解析一级视图,不能识别外部标签JS里面的表达式。

    六、包含

    我们在开发中常常都把页面共同的header和footer提取出来,弄成单独的页面,然后让该包含的页面包含进来,我们就拿footer举例,首先在【templates】下新建一个要背其他页面包含的footer页面【include】:

    <html xmlns:th="http://www.thymeleaf.org">
    <footer th:fragment="footer1">
        <p>All Rights Reserved</p>
    </footer>
    <footer th:fragment="footer2(start,now)">
        <p th:text="|${start} - ${now} All Rights Reserved|"></p>
    </footer>
    </html>

    然后直接在我们的hello.html页面中分别引用上面页面定义好的两个foot:

    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Thymeleaf快速入门-Hello Thymeleaf</title>
    </head>
    <body>
    <div th:include="include::footer1"></div>
    <div th:replace="include::footer2(2015,2018)"></div>
    </body>
    </html>

    刷新页面,可以看到效果:

     

    最后

    以上,就把如何创建运行Spring Boot项目简单的介绍完了,后面会深入介绍Spring Boot的各个功能和用法。

    这个系列课程的完整源码,也会提供给大家。大家关注我的微信公众号(架构师精进),回复:springboot源码。获取这个系列课程的完整源码。

  • 相关阅读:
    UVA12125 March of the Penguins (最大流+拆点)
    UVA 1317 Concert Hall Scheduling(最小费用最大流)
    UVA10249 The Grand Dinner(最大流)
    UVA1349 Optimal Bus Route Design(KM最佳完美匹配)
    UVA1212 Duopoly(最大流最小割)
    UVA1395 Slim Span(kruskal)
    UVA1045 The Great Wall Game(二分图最佳匹配)
    UVA12168 Cat vs. Dog( 二分图最大独立集)
    hdu3488Tour(KM最佳完美匹配)
    UVA1345 Jamie's Contact Groups(最大流+二分)
  • 原文地址:https://www.cnblogs.com/zhangweizhong/p/12398744.html
Copyright © 2011-2022 走看看