Thymeleaf 是一款用于渲染 XML/XHTML/HTML5 內(nèi)容的模板引擎。它類似我們之前使用的 JSP、FreeMaker 等模版,它也可以輕易與 Spring MVC 等 Web 框架進(jìn)行集成作為 Web 應(yīng)用的模板引擎。同時(shí)Thymeleaf得到了Spring Boot官方對(duì)的推薦使用,同時(shí)也是未來(lái)的一個(gè)趨勢(shì)。

與其它模板引擎相比,Thymeleaf最大的特點(diǎn)是能夠直接在瀏覽器中打開(kāi)并正確顯示模板頁(yè)面,而不需要啟動(dòng)整個(gè)Web應(yīng)用。
Thymeleaf的優(yōu)勢(shì)在于
1. 以html的屬性出現(xiàn),保證html的完整語(yǔ)法結(jié)構(gòu)不被破壞;
2. 瀏覽器可直接預(yù)覽模板文件,無(wú)需服務(wù)器端支持;
3. 提供了大量?jī)?nèi)置對(duì)象,內(nèi)置工具類;
4. 語(yǔ)法簡(jiǎn)單,學(xué)習(xí)難度低;
5. 支持html,js,raw等多種模板類型;
Thymeleaf快速入門
1. 在使用Thymeleaf之前,首先在pom.xml文件導(dǎo)入依賴
org.springframework.boot
spring-boot-starter-thymeleaf
2. resource目錄下的templates目錄新建一個(gè)html頁(yè)面test.html(另外Thymeleaf默認(rèn)的頁(yè)面文件后綴是.html)

編寫controller層
package com.markjava.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class UserController {@RequestMapping("/hello") public String test(Model model) { model.addAttribute("name","MarkerJava"); return "test"; }
然后打開(kāi)瀏覽器訪問(wèn)localhost:8080/hello運(yùn)行會(huì)獲取到MarkerJava
注:
th:text在th后面加上一個(gè)冒號(hào),并附加特定的字符組合,這個(gè)thymeleaf定義的占位符是構(gòu)建thymeleaf頁(yè)面的基礎(chǔ)。
th:text="${name}"中的${}是個(gè)占位符${}是個(gè)占位符
如果大家還是沒(méi)有看懂,不太了解的話,可以跟著動(dòng)力節(jié)點(diǎn)的Thymeleaf基礎(chǔ)教程來(lái)學(xué)習(xí),這樣更能直觀的了解,從課程適合在后臺(tái)應(yīng)用,以及各種管理類系統(tǒng)中使用。帶你一步一步詳解講解Thymeleaf模板引擎技術(shù),全面掌握Thymeleaf模板技術(shù),并在實(shí)際項(xiàng)目開(kāi)發(fā)中得心應(yīng)手使用Thymeleaf模板技術(shù)。
?課程目錄:
課程目錄:
第一章Thymeleaf小試牛刀
1-1 thymeleaf-模板工作方式
1-2 thymeleaf-文檔
1-3 thymeleaf-第一個(gè)例子
1-4 thymeleaf-模板使用數(shù)據(jù)
1-5 thymeleaf-使用模板文件
1-6 thymeleaf-設(shè)置模板前綴后綴
1-7 thymeleaf-springboot環(huán)境搭建
1-8 thymeleaf-測(cè)試springboot中使用模板
第二章Thymeleaf語(yǔ)法
2-1 thymeleaf-變量表達(dá)式基本語(yǔ)法-1
2-2 thymeleaf-變量表達(dá)式基本語(yǔ)法-2
2-3 thymeleaf-鏈接表達(dá)式
2-4 thymeleaf-鏈接表達(dá)式傳參數(shù)
2-5 thymeleaf-國(guó)際化介紹
2-6 thymeleaf-自定義LocalResolver
2-7 thymeleaf-國(guó)際化模板定義
2-8 thymeleaf-調(diào)式國(guó)際化
2-9 thymeleaf-文本表達(dá)式
2-10 thymeleaf-數(shù)字表達(dá)式
2-11 thymeleaf-布爾表達(dá)式
2-12 thymeleaf-null和空字符串
2-13 thymeleaf-邏輯表達(dá)式
2-14 thymeleaf-三元運(yùn)算符-1
2-15 thymeleaf-三元運(yùn)算符-2
2-16 thymeleaf-設(shè)置屬性值
2-17 thymeleaf-attr設(shè)置任意屬性值
2-18 thymeleaf-設(shè)置屬性值
2-19 thymeleaf-同時(shí)設(shè)置多個(gè)屬性值
2-20 thymeleaf-boolean設(shè)置
2-21 thymeleaf-設(shè)置標(biāo)簽體文本
2-22 thymeleaf-循環(huán)each(1)
2-23 thymeleaf-each循環(huán)普通Map
2-24 thymeleaf-each循環(huán)ListMap
2-25 thymeleaf-each循環(huán)數(shù)組
2-26 thymeleaf-each循環(huán)給Select組件添加數(shù)據(jù)
2-27 thymeleaf-each循環(huán)的狀態(tài)變量
2-28 thymeleaf-if使用
2-29 thymeleaf-unless使用
第三章Thymeleaf模板引用
3-1 thymeleaf-模版的介紹
3-2 thymeleaf-模版insert使用
3-3 thymeleaf-函數(shù)模版和當(dāng)前頁(yè)面中定義模版
3-4 thymeleaf-使用id引用模版
3-5 thymeleaf-replace和include的對(duì)比
3-6 thymeleaf-常用insert-replace
3-7 thymeleaf-re刪除
3-8 thymeleaf-re其它的范圍值
3-9 thymeleaf-動(dòng)態(tài)指定模版名稱
3-10 thymeleaf-inline在js中獲取數(shù)據(jù)
3-11 thymeleaf-inline和text的比較使用
3-12 thymeleaf-禁用內(nèi)聯(lián)
3-13 thymeleaf-局部變量
第四章Thymeleaf內(nèi)置對(duì)象
4-1 thymeleaf-內(nèi)置工具類
4-2 thymeleaf-內(nèi)置工具類日期和string
4-3 thymeleaf-內(nèi)置對(duì)象#request
4-4 thymeleaf-內(nèi)置對(duì)象#session和#servletContext
4-5 thymeleaf-快捷對(duì)象param
4-6 thymeleaf-快捷對(duì)象session和application
還處于初學(xué)者的同學(xué),能力上可能還學(xué)不到這些,不過(guò)沒(méi)關(guān)系,動(dòng)力節(jié)點(diǎn)還有針對(duì)初學(xué)者進(jìn)階準(zhǔn)備了其它適合的Java視頻課程,等你跟上進(jìn)度后,在看這套教程就非常簡(jiǎn)單,好學(xué)啦。