天天看點

我的thymeleaf學習筆記

一、常用文法

1.th:text 替換文本

<p th:text="${x.a}">被替換文本</p>
           

2.字元串拼接

<p th:text="${'拼接塊'+x.a+'拼接塊'+x.b+'拼接塊'}" >被替換文本</p>
           

3.周遊List

<tr th:each="x:${xList}">
   <th th:text="${x.a}">被替換文本</th>
   <th th:text="${x.b+' '+x.c}" >被替換文本</th>
</tr>
           

4.格式化日期

th:text="${#dates.format(x.y, 'yyyy-MM-dd HH:mm:ss')}"
           

二、引入外部檔案:

1.@{}的方式引入外部檔案會包含項目名

css:    th:href="@{/css/***.css}" target="_blank" rel="external nofollow" 
js:     th:src="@{/js/***.js}"
img:    th:src="@{/img/***.png}"
           

2.include引入檔案

(1)模闆檔案引入layout,并且聲明decorator=“自定義X”

<html xmlns:th="http://www.thymeleaf.org" 
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" 
layout:decorator="web-model">
           

(2)模闆檔案寫入内容,th:fragment=“自定義Y”。ps:th:remove=“tag”為了删掉外邊包圍着的div

<div th:fragment="left" th:remove="tag">
 ***模闆内容***
</div>
           

(3)需要引入的模闆檔案的html

<div th:include="web-model(自定義X)::left(自定義Y)" ></div>
           

(4)結果

“<div th:include="web-model(即自定義X)::left(即自定義Y)" ></div>”   替換成   “***模闆内容***”
           

繼續閱讀