本文參考: Thymeleaf教程
thymeleaf使用:
引入
<html xmlns:th="http://www.thymeleaf.org">
thymeleaf標准表達式:
thymeleaf支持多種表達式:
- 變量表達式: ${...}
- 選擇變量表達式: *{...}
- 連接表達式: @{...}
- 國際化表達式: #{...}
- 片段引用表達式: ~{...}
- 變量表達式
使用${...}包裹的表達式被稱為變量表達式,該表達式具有以下功能:
- 獲取對象的屬性和方法
- 使用內置的基本對象
- 使用內置的工具對象
使用變量表達式還可以使用內置基本對象,獲取內置對象的屬性,調用內置對象的方法。 Thymeleaf 中常用的內置基本對象如下:
- #ctx :上下文對象;
- #vars :上下文變量;
- #locale:上下文的語言環境;
- #request:HttpServletRequest 對象(僅在 Web 應用中可用);
- #response:HttpServletResponse 對象(僅在 Web 應用中可用);
- #session:HttpSession 對象(僅在 Web 應用中可用);
- #servletContext:ServletContext 對象(僅在 Web 應用中可用)。
除了能使用內置的基本對象外,變量表達式還可以使用一些內置的工具對象。
- strings:字符串工具對象,常用方法有:equals、equalsIgnoreCase、length、trim、toUpperCase、toLowerCase、indexOf、substring、replace、startsWith、endsWith,contains 和 containsIgnoreCase 等;
- numbers:數字工具對象,常用的方法有:formatDecimal 等;
- bools:布爾工具對象,常用的方法有:isTrue 和 isFalse 等;
- arrays:數組工具對象,常用的方法有:toArray、length、isEmpty、contains 和 containsAll 等;
- lists/sets:List/Set 集合工具對象,常用的方法有:toList、size、isEmpty、contains、containsAll 和 sort 等;
- maps:Map 集合工具對象,常用的方法有:size、isEmpty、containsKey 和 containsValue 等;
- dates:日期工具對象,常用的方法有:format、year、month、hour 和 createNow 等。
thymeleaf屬性使用:
th:text 和 th:utext使用:
<!-- th:text 為Thymeleaf屬性,用於展示純文本,會對特殊字符進行轉義--> <!-- 經過thymeleaf解析后,會將原來的內容進行替換 --> <div th:text="hello">world</div> <!-- th:utext 文本替換,不轉義特殊字符 --> <div th:utext="hello" >world</div>
結果:
<!-- th:text和th:utext的異同: 相同: 1.都可以對變量或表達式進行求值 2.用“ + ”可進行文本連接 不同: 當獲取從后段傳來的參數帶有html標簽時 th:text不會進行解析 th:utext進行解析 --> <div th:text="2>1"></div> <div th:utext="2>1"></div> <div th:text="'hello ' + 'world'"></div> <div th:utext="'hello ' + 'world'"></div> <div th:text="${msg}"></div> <div th:utext="${msg}"></div> <div><span>helloworld</span></div>
th:id:
<!-- th:id 替換id屬性 --> <div id="id1" th:id="thymeleaf-id"></div>
控制台結果
th:value:
<!-- th:value 替換value屬性 --> <input type="text" value="input" th:value="thymeleaf_value"/>
結果
th:object :
<!-- th:object 在父標簽選擇對象,在字標簽中使用 *{}選擇表達式選取值 若父標簽沒有選擇對象,字標簽使用*{}選擇表達式或${}變量表達式效果是一樣的, 同時父標簽選擇了對象,字標簽仍可用${}變量表達式取值 --> <div th:object="${user}"> 第一:<span th:text="*{username}"></span> </div> <div> 第二:<span th:text="*{user.username}"></span> </div> <div th:object="${user}"> 第三:<span th:text="${user.username}"></span> </div>
結果:
th:with :
1 <!-- th:with局部變量賦值運算 --> 2 <span th:with="a = 'hello world'" th:text="${a}"></span>
結果:
th:style:
1 <!-- th:style設置樣式 --> 2 <span th:style=" 'color:red;font-size:16px;' ">設置樣式</span>
結果:
經處理后的html加入了行內樣式:
th:onclick:
1 <!-- th:onclick點擊事件 --> 2 <input type="button" value="提交" th:onclick=" 'submit()' " />
經處理后的html:
th:each:
請看:thymeleaf th:each使用 - 基地您 - 博客園 (cnblogs.com)
th:if
th:unless
th:switch :
1 <!-- 2 th:if 根據條件判斷是否要展示此標簽 3 th:unless 與th:if判斷條件相反,當條件不滿足時顯示 4 th:switch 與Java Switch語句類似,搭配th:case使用, 5 根據不同條件展示不同內容 6 --> 7 <div th:if="'a' == 'b'"> a == b</div> 8 <div th:if=" 'a' eq 'a'">a eq a</div> 9 <div th:unless=" 1==2 ">th:unless 1==2 </div> 10 <div th:with="a = '3'" th:switch="${a}" > 11 <span th:case="1">case = 1</span> 12 <span th:case="3">case =3</span> 13 <span th:case="2">case =2</span> 14 </div>
結果:
th:selected
th:src
th:inline:
1 <!-- 2 th:selected select 選擇框選中使用 3 th:src 替換src屬性 4 th:inline 內聯屬性; 5 該屬性有 text,none,javascript 三種取值, 6 在 <script> 標簽中使用時,js 代碼中可以獲取到后台傳遞頁面的對象。 7 8 內聯方法: 通常我們使用thymeleaf屬性th... + ${} 獲取屬性值,如果我們需要在標簽內獲取變量就需要使用內聯樣式 9 使用 [[${屬性名}]] 或 [(${屬性名})]可在標簽內獲取變量值 10 如: 有一個name = 'world', 使用內聯 <span>hello , [[${name}]]</span> 11 結果為: hello , world 12 但js代碼中可能會將js的數組進行解析,此時需要將th:inline設置為none,使其不對js代碼進行解析 13 --> 14 <select th:with="a = 3"> 15 <option th:selected="${a==1}">- 1 -</option> 16 <option th:selected="${a==2}">- 2 -</option> 17 <option th:selected="${a==3}">- 3 -</option> 18 </select> 19 <img src="http://www.baidu.com" th:src="@{/wode}"/> 20 <script th:with="a='zhangsan' " type="text/javascript" th:inline="javascript"> 21 var name = [[${a}]]; 22 </script>
經過解析后的html代碼:
th:inline 詳情請看: https://blog.csdn.net/fanpeizhong/article/details/80173452
th:fragment 模板布局,類似 JSP 的 tag,用來定義一段被引用或包含的模板片段
- <footer th:fragment="footer">插入的內容</footer>
th:insert 布局標簽;
將使用 th:fragment 屬性指定的模板片段(包含標簽)插入到當前標簽中。
- <div th:insert="commons/bar::footer"></div>
th:replace 布局標簽;
使用 th:fragment 屬性指定的模板片段(包含標簽)替換當前整個標簽。
- <div th:replace="commons/bar::footer"></div>
th:action 替換表單提交地址
- <form th:action="@{/user/login}" th:method="post"></form>
Thymeleaf公共頁面抽取請看: Thymeleaf公共代碼段抽取,th:include, th:replace, th:insert - 基地您 - 博客園 (cnblogs.com)