1. 程式人生 > >我的thymeleaf語法總結

我的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')}"


二、引入外部檔案:

[email protected]{}的方式引入外部檔案會包含專案名
css:    th:href="@{/css/***.css}"
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>”   替換成   “***模板內容***”