天天看點

Spring Boot Thymeleaf

Thymeleaf: 通過自身特定文法對html的标記做渲染。

Thymeleaf 用法

  • th:text在頁面中輸出值
  • th:value可以将一個值放入到input标簽的value中。

<input type="text" name="name" th:value="${name}" />

  • th:if條件判斷

<span th:if="${name} == 'world'" >

    1111111

</span>

<span th:if="${name} == 'World'" >

    2222

</span>

name=“world”  時 <span>111111</span>

name=“World”時 <span>2222</span>

  • th:switch
  • th:strings函數
函數名 作用
${#strings.indexOf(msg, 'h')} 查找子串的位置,并傳回該子串的下标,如果沒找到則傳回-1。
${#strings.isEmpty(msg)} 判斷msg是否為空,如果為空則傳回true,否則傳回false 。
${#strings.toUppercase(msg)} 字元串(msg)轉為大寫
${#strings.toLoweercase(msg)} 字元串(msg)轉為小寫
${#strings.substring(msg, 13)} 截取msg(0-13位)的子串
${#strings.substring(msg, 13, 15)} 截取msg(13位開始-15位結束)的子串。
${#strings.indexOf(msg, ‘h‘)} 查找子串(h)的位置,h存在則傳回下标,否則傳回-1。
${#strings.length(key)} 傳回字元串長度
${#strings.endsWith(key, 'T')} 判斷字元串key是否以子串T結尾,如果是傳回true,否則傳回false。
${#strings.startsWith(key, 'T')} 判斷字元串key是否以子串T開始,如果是傳回true,否則傳回false。
${#strings.contains(key, 'T')} 判斷字元串是否包含子串T,如果包含則傳回true,否則傳回false。
  • th:strings函數
  1. Strings函數用法與JDK String類的函數用法相同。
  2. 調用thymeleaf内置對象一定要用#
  3. 大部分的内置對象都以s結尾,例如:strings,numbers,dates
  • ​​​​​​​th:each
Spring Boot Thymeleaf
Spring Boot Thymeleaf
  • th:each狀态變量
Spring Boot Thymeleaf
Spring Boot Thymeleaf
Spring Boot Thymeleaf
  • th:each疊代map
Spring Boot Thymeleaf
Spring Boot Thymeleaf
Spring Boot Thymeleaf
  • th傳遞對象
Spring Boot Thymeleaf
Spring Boot Thymeleaf
Spring Boot Thymeleaf
  • th:ref
  • 絕對路徑 <a th:ref=“@{http://www.baidu.com}”>絕對路徑</a>
  • 相對路徑
  • 相對有項目的上下文目錄的相對路徑

<a th:ref=“@{/show}”>相對路徑</a>

  • 相對于伺服器路徑的根

 <a th:ref=“@{~/projects/resourcname}”>相對于伺服器的根</a>

Spring Boot Thymeleaf
Spring Boot Thymeleaf
  • th:href傳參