使用Thymeleaf 三大理由:
- 簡(jiǎn)潔漂亮 容易理解
- 完美支持HTML5 使用瀏覽器直接打開(kāi)頁(yè)面
- 不新增標(biāo)簽 只需增強(qiáng)屬性
學(xué)習(xí)目標(biāo)
- 快速掌握Thymeleaf的基本使用(五大基礎(chǔ)語(yǔ)法+常用內(nèi)置對(duì)象)
快速查閱
專題閱讀:《SpringBoot 布道系列》
源碼下載:springboot-web-thymeleaf-enhance
— Hey Man衷畦,Don't forget to Star or Fork . —
官方指南: Thymleaf 3.0 官方教程
相關(guān)教程:SpringBoot Thymeleaf 基本介紹 、 SpringBoot 在IDEA中實(shí)現(xiàn)熱部署(實(shí)用版)
使用教程
溫馨提示:Thymeleaf 最為顯著的特征是增強(qiáng)屬性驾霜,任何屬性都可以通過(guò)th:xx 來(lái)完成交互乎串,例如th:value最終會(huì)覆蓋value屬性店枣。
一、基礎(chǔ)語(yǔ)法
變量表達(dá)式 ${}
使用方法:直接使用th:xx = "${}"
獲取對(duì)象屬性 。例如:
<form id="userForm">
<input id="id" name="id" th:value="${user.id}"/>
<input id="username" name="username" th:value="${user.username}"/>
<input id="password" name="password" th:value="${user.password}"/>
</form>
<div th:text="hello"></div>
<div th:text="${user.username}"></div>
選擇變量表達(dá)式 *{}
使用方法:首先通過(guò)th:object
獲取對(duì)象鸯两,然后使用th:xx = "*{}"
獲取對(duì)象屬性闷旧。
這種簡(jiǎn)寫(xiě)風(fēng)格極為清爽,推薦大家在實(shí)際項(xiàng)目中使用钧唐。 例如:
<form id="userForm" th:object="${user}">
<input id="id" name="id" th:value="*{id}"/>
<input id="username" name="username" th:value="*{username}"/>
<input id="password" name="password" th:value="*{password}"/>
</form>
鏈接表達(dá)式 @{}
使用方法:通過(guò)鏈接表達(dá)式@{}
直接拿到應(yīng)用路徑忙灼,然后拼接靜態(tài)資源路徑。例如:
<script th:src="@{/webjars/jquery/jquery.js}"></script>
<link th:href="@{/webjars/bootstrap/css/bootstrap.css}" rel="stylesheet" type="text/css">
片段表達(dá)式 ~{}
片段表達(dá)式是Thymeleaf的特色之一钝侠,細(xì)粒度可以達(dá)到標(biāo)簽級(jí)別该园,這是JSP無(wú)法做到的。
片段表達(dá)式擁有三種語(yǔ)法:
~{ viewName } 表示引入完整頁(yè)面
~{ viewName ::selector} 表示在指定頁(yè)面尋找片段 其中selector可為片段名帅韧、jquery選擇器等
~{ ::selector} 表示在當(dāng)前頁(yè)尋找
使用方法:首先通過(guò)th:fragment
定制片段 里初,然后通過(guò)th:replace
填寫(xiě)片段路徑和片段名。例如:
<!-- /views/common/head.html-->
<head th:fragment="static">
<script th:src="@{/webjars/jquery/3.3.1/jquery.js}"></script>
</head>
<!-- /views/your.html -->
<div th:replace="~{common/head::static}"></div>
在實(shí)際使用中忽舟,我們往往使用更簡(jiǎn)潔的表達(dá)双妨,去掉表達(dá)式外殼直接填寫(xiě)片段名。例如:
<!-- your.html -->
<div th:replace="common/head::static"></div>
值得注意的是叮阅,使用替換路徑th:replace
開(kāi)頭請(qǐng)勿添加斜杠斥难,避免部署運(yùn)行的時(shí)候出現(xiàn)路徑報(bào)錯(cuò)。(因?yàn)槟J(rèn)拼接的路徑為spring.thymeleaf.prefix = classpath:/templates/
)
消息表達(dá)式
即通常的國(guó)際化屬性:#{msg}
用于獲取國(guó)際化語(yǔ)言翻譯值帘饶。例如:
<title th:text="#{user.title}"></title>
其它表達(dá)式
在基礎(chǔ)語(yǔ)法中哑诊,默認(rèn)支持字符串連接、數(shù)學(xué)運(yùn)算及刻、布爾邏輯和三目運(yùn)算等镀裤。例如:
<input name="name" th:value="${'I am '+(user.name!=null?user.name:'NoBody')}"/>
二、內(nèi)置對(duì)象
官方文檔: 附錄A: Thymeleaf 3.0 基礎(chǔ)對(duì)象
官方文檔: 附錄B: Thymeleaf 3.0 工具類
七大基礎(chǔ)對(duì)象:
-
${#ctx}
上下文對(duì)象缴饭,可用于獲取其它內(nèi)置對(duì)象暑劝。 -
${#vars}
: 上下文變量。 -
${#locale}
:上下文區(qū)域設(shè)置颗搂。 -
${#request}
: HttpServletRequest對(duì)象担猛。 -
${#response}
: HttpServletResponse對(duì)象。 -
${#session}
: HttpSession對(duì)象丢氢。 -
${#servletContext}
: ServletContext對(duì)象傅联。
常用的工具類:
-
#strings
:字符串工具類 -
#lists
:List 工具類 -
#arrays
:數(shù)組工具類 -
#sets
:Set 工具類 -
#maps
:常用Map方法。 -
#objects
:一般對(duì)象類疚察,通常用來(lái)判斷非空 -
#bools
:常用的布爾方法蒸走。 -
#execInfo
:獲取頁(yè)面模板的處理信息。 -
#messages
:在變量表達(dá)式中獲取外部消息的方法貌嫡,與使用#{...}語(yǔ)法獲取的方法相同比驻。 -
#uris
:轉(zhuǎn)義部分URL / URI的方法该溯。 -
#conversions
:用于執(zhí)行已配置的轉(zhuǎn)換服務(wù)的方法。 -
#dates
:時(shí)間操作和時(shí)間格式化等别惦。 -
#calendars
:用于更復(fù)雜時(shí)間的格式化狈茉。 -
#numbers
:格式化數(shù)字對(duì)象的方法。 -
#aggregates
:在數(shù)組或集合上創(chuàng)建聚合的方法掸掸。 -
#ids
:處理可能重復(fù)的id屬性的方法氯庆。
三、迭代循環(huán)
想要遍歷List
集合很簡(jiǎn)單猾漫,配合th:each
即可快速完成迭代。例如遍歷用戶列表:
<div th:each="user:${userList}">
賬號(hào):<input th:value="${user.username}"/>
密碼:<input th:value="${user.password}"/>
</div>
在集合的迭代過(guò)程還可以獲取狀態(tài)變量感凤,只需在變量后面指定狀態(tài)變量名即可悯周,狀態(tài)變量可用于獲取集合的下標(biāo)/序號(hào)、總數(shù)陪竿、是否為單數(shù)/偶數(shù)行禽翼、是否為第一個(gè)/最后一個(gè)。例如:
<div th:each="user,stat:${userList}" th:class="${stat.even}?'even':'odd'">
下標(biāo):<input th:value="${stat.index}"/>
序號(hào):<input th:value="${stat.count}"/>
賬號(hào):<input th:value="${user.username}"/>
密碼:<input th:value="${user.password}"/>
</div>
如果缺省狀態(tài)變量名族跛,則迭代器會(huì)默認(rèn)幫我們生成以變量名開(kāi)頭的狀態(tài)變量 xxStat
闰挡, 例如:
<div th:each="user:${userList}" th:class="${userStat.even}?'even':'odd'">
下標(biāo):<input th:value="${userStat.index}"/>
序號(hào):<input th:value="${userStat.count}"/>
賬號(hào):<input th:value="${user.username}"/>
密碼:<input th:value="${user.password}"/>
</div>
四、條件判斷
條件判斷通常用于動(dòng)態(tài)頁(yè)面的初始化礁哄,例如:
<div th:if="${userList}">
<div>的確存在..</div>
</div>
如果想取反則使用unless 例如:
<div th:unless="${userList}">
<div>不存在..</div>
</div>
五长酗、日期格式化
使用默認(rèn)的日期格式(toString方法) 并不是我們預(yù)期的格式:Mon Dec 03 23:16:50 CST 2018
<input type="text" th:value="${user.createTime}"/>
此時(shí)可以通過(guò)時(shí)間工具類#dates
來(lái)對(duì)日期進(jìn)行格式化:2018-12-03 23:16:50
<input type="text" th:value="${#dates.format(user.createTime,'yyyy-MM-dd HH:mm:ss')}"/>
六、內(nèi)聯(lián)寫(xiě)法
(1)為什么要使用內(nèi)聯(lián)寫(xiě)法桐绒?·答:因?yàn)?JS無(wú)法獲取服務(wù)端返回的變量夺脾。
(2)如何使用內(nèi)聯(lián)表達(dá)式?答:標(biāo)準(zhǔn)格式為:
[[${xx}]]
茉继,可以讀取服務(wù)端變量咧叭,也可以調(diào)用內(nèi)置對(duì)象的方法。例如獲取用戶變量和應(yīng)用路徑:
<script th:inline="javascript">
var user = [[${user}]];`
var APP_PATH = [[${#request.getContextPath()}]];
var LANG_COUNTRY = [[${#locale.getLanguage()+'_'+#locale.getCountry()}]];
</script>
- (3)標(biāo)簽引入的JS里面能使用內(nèi)聯(lián)表達(dá)式嗎烁竭?答:不能菲茬!內(nèi)聯(lián)表達(dá)式僅在頁(yè)面生效,因?yàn)?code>Thymeleaf只負(fù)責(zé)解析一級(jí)視圖派撕,不能識(shí)別外部標(biāo)簽JS里面的表達(dá)式婉弹。
七、國(guó)際化
需要了解更多關(guān)于國(guó)際化的精彩描述請(qǐng)前往 SpringBoot 快速實(shí)現(xiàn)國(guó)際化i18n 终吼。
例如在國(guó)際化文件中編寫(xiě)了user.title這個(gè)鍵值马胧,然后使用#{}
讀取這個(gè)KEY即可獲取翻譯。
<title th:text="#{user.title}">用戶登陸</title>
八衔峰、詳細(xì)教程
======== 有了上述基礎(chǔ)后 下面正式開(kāi)始Thymeleaf 的詳細(xì)教程 ==============
首先通過(guò)Spring Initializr創(chuàng)建項(xiàng)目佩脊,如圖所示:
然后在POM文件引入web 蛙粘、thymeleaf
等依賴:
<dependencies>
<dependency><!--Web相關(guān)依賴-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><!--頁(yè)面模板依賴-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency><!--熱部署依賴-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
然后在src/main/resources/application.yml
配置頁(yè)面路徑:
spring:
thymeleaf:
cache: false #關(guān)閉緩存
prefix: classpath:/views/ #調(diào)整頁(yè)面路徑
接著在src/main/java/com/hehe/web/UserController
獲取用戶信息:
@RestController
public class UserController {
private List<User> userList = new ArrayList<>();
{
userList.add(new User("1", "socks", "123456", new Date()));
userList.add(new User("2", "admin", "111111", new Date()));
userList.add(new User("3", "jacks", "222222", null));
}
@GetMapping("/")
public ModelAndView index() {
return new ModelAndView("user/user", "userList", userList);
}
}
public class User {
private String id;
private String username;
private String password;
private Date createTime;
//請(qǐng)讀者自行補(bǔ)充 構(gòu)造器和 get/set方法..
}
開(kāi)始編寫(xiě)公共頁(yè)面:src/main/resources/views/common/head.html
,其中static
為頁(yè)面片段:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<!--聲明static為頁(yè)面片段名稱-->
<head th:fragment="static">
<link th:href="@{/webjars/bootstrap/css/bootstrap.css}" rel="stylesheet" type="text/css"/>
<script th:src="@{/webjars/jquery/jquery.js}"></script>
</head>
</html>
接著編寫(xiě)用戶列表頁(yè):src/main/resources/views/user/user.html
配合th:each
顯示用戶列表信息威彰。
使用說(shuō)明:這里 th:replace="common/head::static"
表示將引用${spring.thymeleaf.prefix}/common/head.html
的static
頁(yè)面片段出牧,值得注意的是由于替換路徑默認(rèn)會(huì)拼接前綴路徑,所以開(kāi)頭切勿在添加斜杠歇盼,否則在打包成JAR部署運(yùn)行時(shí)將提示報(bào)Templates not found...
舔痕。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title th:text="用戶信息">User</title>
<!--默認(rèn)拼接前綴路徑,開(kāi)頭請(qǐng)勿再添加斜杠,防止部署運(yùn)行報(bào)錯(cuò)!-->
<script th:replace="common/head::static"></script>
</head>
<body>
<div th:each="user,userStat:${userList}" th:class="${userStat.even}?'even':'odd'">
序號(hào):<input type="text" th:value="${userStat.count}"/>
賬號(hào):<input type="text" th:value="${user.username}"/>
密碼:<input type="password" th:value="${user.password}"/>
時(shí)間:<input type="text" th:value="${user.createTime}"/>
時(shí)間:<input type="text" th:value="${#dates.format(user.createTime,'yyyy-MM-dd HH:mm:ss')}"/>
</div>
<script th:inline="javascript">
//通過(guò)內(nèi)聯(lián)表達(dá)式獲取用戶信息
var userList = [[${userList}]];
console.log(userList)
</script>
</body>
</html>
然后編寫(xiě)單個(gè)用戶頁(yè)面:
至此大功告成豹缀,然后快速啟動(dòng)項(xiàng)目伯复,如圖所示:
然后訪問(wèn)用戶列表: http://localhost:8080 ,如圖所示:
然后訪問(wèn)單個(gè)用戶: http://localhost:8080/user/1 邢笙,如圖所示: