Thymeleaf語法總結
一、Thymeleaf介紹
Thymeleaf是Spring boot推薦使用的模版引擎,直接以html顯示,前后端可以很好的分離,
二、Thymeleaf語法(Thymeleaf3)
在使用Thymeleaf時頁面要引入名稱空間: xmlns:th="http://www.thymeleaf.org" 1、th屬性,常用th屬性如下: 1)th:text:文本替換;<h1 th:text="${session.vel2}"></h1>
2)th:utext:支持html的文本替換,
3)th:value:屬性賦值
4)th:each:遍歷回圈元素
//每次遍歷都會產生th:each所標注的標簽以及內部標簽
<tr th:each="user : ${users}">
<td th:text="${user.name}">Onions</td>
<td th:text="${user.age}">2.41</td>
</tr>
5)th:if:判斷條件,類似的還有th:unless,th:switch,th:case
<div th:if="true">
你填的是true
</div>
//這里參考了一個th:if指令,跟vue中的v-if類似
6)th:insert:代碼塊引入,類似的還有th:replace,th:include,常用于公共代碼塊提取的場景
7)th:fragment:定義代碼塊,方便被th:insert參考
8)th:object:宣告變數,一般和*{}一起配合使用,達到偷懶的效果,
<h2 th:object="${user}">
<p>Name: <span th:text="*{name}">Jack</span>.</p>
<p>Age: <span th:text="*{age}">21</span>.</p>
<p>friend: <span th:text="*{friend.name}">Rose</span>.</p>
</h2>
9)th:attr:設定標簽屬性,多個屬性可以用逗號分隔
例子:
前端HTML
<!DOCTYPE html><!--名稱空間-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Thymeleaf 語法</title>
</head>
<body>
<h2>ITDragon Thymeleaf 語法</h2>
<!--th:text 設定當前元素的文本內容,常用,優先級不高-->
<p th:text="${thText}" />
<p th:utext="${thUText}" />
<!--th:value 設定當前元素的value值,常用,優先級僅比th:text高-->
<input type="text" th:value="https://www.cnblogs.com/smart-rick/archive/2021/01/02/${thValue}" />
<!--th:each 遍歷串列,常用,優先級很高,僅此于代碼塊的插入-->
<!--th:each 修飾在div上,則div層重復出現,若只想p標簽遍歷,則修飾在p標簽上-->
<div th:each="message : ${thEach}"> <!-- 遍歷整個div-p,不推薦-->
<p th:text="${message}" />
</div>
<div> <!--只遍歷p,推薦使用-->
<p th:text="${message}" th:each="message : ${thEach}" />
</div>
<!--th:if 條件判斷,類似的有th:switch,th:case,優先級僅次于th:each, 其中#strings是變數運算式的內置方法-->
<p th:text="${thIf}" th:if="${not #strings.isEmpty(thIf)}"></p>
<!--th:insert 把代碼塊插入當前div中,優先級最高,類似的有th:replace,th:include,~{} :代碼塊運算式 -->
<div th:insert="~{grammar/common::thCommon}"></div>
<!--th:object 宣告變數,和*{} 一起使用-->
<div th:object="${thObject}">
<p>ID: <span th:text="*{id}" /></p><!--th:text="${thObject.id}"-->
<p>TH: <span th:text="*{thName}" /></p><!--${thObject.thName}-->
<p>DE: <span th:text="*{desc}" /></p><!--${thObject.desc}-->
</div>
</body>
</html>
后臺controller:
@Controller
public class ThymeleafController {
@RequestMapping("thymeleaf")
public String thymeleaf(ModelMap map) {
map.put("thText", "th:text 設定文本內容 <b>加粗</b>");
map.put("thUText", "th:utext 設定文本內容 <b>加粗</b>");
map.put("thValue", "thValue 設定當前元素的value值");
map.put("thEach", Arrays.asList("th:each", "遍歷串列"));
map.put("thIf", "msg is not null");
map.put("thObject", new ThObject(1L, "th:object", "用來偷懶的th屬性"));
return "grammar/thymeleaf";
}
}
2、標準運算式語法:
- ${...} 變數運算式,Variable Expressions
- @{...} 鏈接運算式,Link URL Expressions
- #{...} 訊息運算式,Message Expressions
- ~{...} 代碼塊運算式,Fragment Expressions
- *{...} 選擇變數運算式,Selection Variable Expressions
-
~{...} 代碼塊運算式,支持兩種語法結構
代碼塊運算式的使用
代碼塊運算式需要配合th屬性(th:insert,th:replace,th:include)一起使用, th:insert:將代碼塊片段整個插入到使用了th:insert的HTML標簽中, th:replace:將代碼塊片段整個替換使用了th:replace的HTML標簽中, th:include:將代碼塊片段包含的內容插入到使用了th:include的HTML標簽中,#{...} 訊息運算式
訊息運算式一般用于國際化的場景,@{...} 鏈接運算式
鏈接運算式好處 不管是靜態資源的參考,form表單的請求,凡是鏈接都可以用@{...} ,這樣可以動態獲取專案路徑,即便專案名變了,依然可以正常訪問 #修改專案名,鏈接運算式會自動修改路徑,避免資源檔案找不到 server.context-path=/itdragon 鏈接運算式結構 無參:@{/xxx} 有參:@{/xxx(k1=v1,k2=v2)} 對應url結構:xxx?k1=v1&k2=v2 引入本地資源:@{/專案本地的資源路徑} 引入外部資源:@{/webjars/資源在jar包中的路徑} 列舉:<link th:href="https://www.cnblogs.com/smart-rick/archive/2021/01/02/@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">
<link th:href="https://www.cnblogs.com/smart-rick/archive/2021/01/02/@{/main/css/itdragon.css}" rel="stylesheet">
<form th:action="@{/user/login}" th:method="post" >
<a th:href="https://www.cnblogs.com/smart-rick/archive/2021/01/02/@{/login.html(l='zh_CN')}">中文</a>
<a th:href="https://www.cnblogs.com/smart-rick/archive/2021/01/02/@{/login.html(l='en_US')}">English</a>
${...} 變數運算式功能
一、可以獲取物件的屬性和方法 二、可以使用ctx,vars,locale,request,response,session,servletContext內置物件 三、可以使用dates,numbers,strings,objects,arrays,lists,sets,maps等內置方法(重點介紹)常用的內置物件
一、ctx :背景關系物件, 二、vars :背景關系變數, 三、locale:背景關系的語言環境, 四、request:(僅在web背景關系)的 HttpServletRequest 物件, 五、response:(僅在web背景關系)的 HttpServletResponse 物件, 六、session:(僅在web背景關系)的 HttpSession 物件, 七、servletContext:(僅在web背景關系)的 ServletContext 物件 這里以常用的Session舉例,用戶刊登成功后,會把用戶資訊放在Session中,Thymeleaf通過內置物件將值從session中獲取, // java 代碼將用戶名放在session中 session.setAttribute("userinfo",username); // Thymeleaf通過內置物件直接獲取 th:text="${session.userinfo}"常用的內置方法
一、strings:字串格式化方法,常用的Java方法它都有,比如: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:集合方法,常用的方法有:toList,size,isEmpty,contains,containsAll,sort等 六、maps:物件方法,常用的方法有:size,isEmpty,containsKey,containsValue等 七、dates:日期方法,常用的方法有:format,year,month,hour,createNow等 1 <!DOCTYPE html><html lang="en" xmlns:th="http://www.thymeleaf.org"><head>
2 <meta charset="UTF-8">
3 <title>ITDragon Thymeleaf 內置方法</title></head><body>
4 <h2>ITDragon Thymeleaf 內置方法</h2>
5 <h3>#strings </h3>
6 <div th:if="${not #strings.isEmpty(itdragonStr)}" >
7 <p>Old Str : <span th:text="${itdragonStr}"/></p>
8 <p>toUpperCase : <span th:text="${#strings.toUpperCase(itdragonStr)}"/></p>
9 <p>toLowerCase : <span th:text="${#strings.toLowerCase(itdragonStr)}"/></p>
10 <p>equals : <span th:text="${#strings.equals(itdragonStr, 'itdragonblog')}"/></p>
11 <p>equalsIgnoreCase :
12 <span th:text="${#strings.equalsIgnoreCase(itdragonStr, 'itdragonblog')}"/></p>
13 <p>indexOf : <span th:text="${#strings.indexOf(itdragonStr, 'r')}"/></p>
14 <p>substring : <span th:text="${#strings.substring(itdragonStr, 2, 8)}"/></p>
15 <p>replace : <span th:text="${#strings.replace(itdragonStr, 'it', 'IT')}"/></p>
16 <p>startsWith : <span th:text="${#strings.startsWith(itdragonStr, 'it')}"/></p>
17 <p>contains : <span th:text="${#strings.contains(itdragonStr, 'IT')}"/></p>
18 </div>
19
20 <h3>#numbers </h3>
21 <div>
22 <p>formatDecimal 整數部分隨意,小數點后保留兩位,四舍五入:
23 <span th:text="${#numbers.formatDecimal(itdragonNum, 0, 2)}"/></p>
24 <p>formatDecimal 整數部分保留五位數,小數點后保留兩位,四舍五入:
25 <span th:text="${#numbers.formatDecimal(itdragonNum, 5, 2)}"/></p>
26 </div>
27
28 <h3>#bools </h3>
29 <div th:if="${#bools.isTrue(itdragonBool)}">
30 <p th:text="${itdragonBool}"></p>
31 </div>
32
33 <h3>#arrays </h3>
34 <div th:if="${not #arrays.isEmpty(itdragonArray)}">
35 <p>length : <span th:text="${#arrays.length(itdragonArray)}"/></p>
36 <p>contains : <span th:text="${#arrays.contains(itdragonArray, 5)}"/></p>
37 <p>containsAll : <span th:text="${#arrays.containsAll(itdragonArray, itdragonArray)}"/></p>
38 </div>
39 <h3>#lists </h3>
40 <div th:if="${not #lists.isEmpty(itdragonList)}">
41 <p>size : <span th:text="${#lists.size(itdragonList)}"/></p>
42 <p>contains : <span th:text="${#lists.contains(itdragonList, 0)}"/></p>
43 <p>sort : <span th:text="${#lists.sort(itdragonList)}"/></p>
44 </div>
45 <h3>#maps </h3>
46 <div th:if="${not #maps.isEmpty(itdragonMap)}">
47 <p>size : <span th:text="${#maps.size(itdragonMap)}"/></p>
48 <p>containsKey : <span th:text="${#maps.containsKey(itdragonMap, 'thName')}"/></p>
49 <p>containsValue : <span th:text="${#maps.containsValue(itdragonMap, '#maps')}"/></p>
50 </div>
51 <h3>#dates </h3>
52 <div>
53 <p>format : <span th:text="${#dates.format(itdragonDate)}"/></p>
54 <p>custom format : <span th:text="${#dates.format(itdragonDate, 'yyyy-MM-dd HH:mm:ss')}"/></p>
55 <p>day : <span th:text="${#dates.day(itdragonDate)}"/></p>
56 <p>month : <span th:text="${#dates.month(itdragonDate)}"/></p>
57 <p>monthName : <span th:text="${#dates.monthName(itdragonDate)}"/></p>
58 <p>year : <span th:text="${#dates.year(itdragonDate)}"/></p>
59 <p>dayOfWeekName : <span th:text="${#dates.dayOfWeekName(itdragonDate)}"/></p>
60 <p>hour : <span th:text="${#dates.hour(itdragonDate)}"/></p>
61 <p>minute : <span th:text="${#dates.minute(itdragonDate)}"/></p>
62 <p>second : <span th:text="${#dates.second(itdragonDate)}"/></p>
63 <p>createNow : <span th:text="${#dates.createNow()}"/></p>
64 </div></body></html>
1 @RequestMapping("varexpressions")public String varexpressions(ModelMap map) {
2 map.put("itdragonStr", "itdragonBlog");
3 map.put("itdragonBool", true);
4 map.put("itdragonArray", new Integer[]{1,2,3,4});
5 map.put("itdragonList", Arrays.asList(1,3,2,4,0));
6 Map itdragonMap = new HashMap();
7 itdragonMap.put("thName", "${#...}");
8 itdragonMap.put("desc", "變數運算式內置方法");
9 map.put("itdragonMap", itdragonMap);
10 map.put("itdragonDate", new Date());
11 map.put("itdragonNum", 888.888D); return "grammar/varexpressions";
12 }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/243779.html
標籤:其他
上一篇:機器學習 - 相關概念與實作流程
下一篇:最全正則運算式
