前言
??thymeleaf是一個XML/XHTML/HTML5模板引擎脐湾,可用于Web與非Web環(huán)境中的應用開發(fā)器躏。它是一個開源的Java庫截酷,基于Apache License 2.0許可诗祸,由Daniel Fernández創(chuàng)建镀虐,該作者還是Java加密庫Jasypt的作者屹培。
??Thymeleaf提供了一個用于整合Spring MVC的可選模塊默穴,在應用開發(fā)中,你可以使用Thymeleaf來完全代替JSP或其他模板引擎褪秀,如Velocity蓄诽、FreeMarker等。Thymeleaf的主要目標在于提供一種可被瀏覽器正確顯示的媒吗、格式良好的模板創(chuàng)建方式仑氛,因此也可以用作靜態(tài)建模。你可以使用它創(chuàng)建經(jīng)過驗證的XML與HTML模板。相對于編寫邏輯或代碼锯岖,開發(fā)者只需將標簽屬性添加到模板中即可介袜。接下來,這些標簽屬性就會在DOM(文檔對象模型)上執(zhí)行預先制定好的邏輯出吹。
??它的特點便是:開箱即用遇伞,Thymeleaf允許您處理六種模板,每種模板稱為模板模式:
??1.XML
??2.有效的XML
??3.XHTML
??4.有效的XHTML
??5.HTML5
??6.舊版HTML5
??所有這些模式都指的是格式良好的XML文件捶牢,但Legacy HTML5模式除外鸠珠,它允許您處理HTML5文件,其中包含獨立(非關(guān)閉)標記秋麸,沒有值的標記屬性或不在引號之間寫入的標記屬性渐排。為了在這種特定模式下處理文件,Thymeleaf將首先執(zhí)行轉(zhuǎn)換灸蟆,將您的文件轉(zhuǎn)換為格式良好的XML文件驯耻,這些文件仍然是完全有效的HTML5(實際上是創(chuàng)建HTML5代碼的推薦方法)
另請注意,驗證僅適用于XML和XHTML模板次乓。
??然而吓歇,這些并不是Thymeleaf可以處理的唯一模板類型,并且用戶始終能夠通過指定在此模式下解析模板的方法和編寫結(jié)果的方式來定義他/她自己的模式票腰。這樣城看,任何可以建模為DOM樹(無論是否為XML)的東西都可以被Thymeleaf有效地作為模板處理。
Springboot整合thymeleaf
pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
application.yml:
thymeleaf:
cache: false # 不加載緩存
controller:
@Controller
@RequestMapping("/demo")
public class DemoController {
@GetMapping("/test")
public String test(Model model,String id){
System.out.println(id);
List<User> userList = new ArrayList<>();
userList.add(new User(1,"zs","bj"));
userList.add(new User(2,"ls","hlj"));
userList.add(new User(3,"ww","haerbin"));
model.addAttribute("userList",userList);
Map<String,Object> dataMap = new HashMap<>();
dataMap.put("No","123");
dataMap.put("address","bj");
model.addAttribute("dataMap",dataMap);
String[] names = {"張三","李四","王五"};
model.addAttribute("names",names);
model.addAttribute("now",new Date());
model.addAttribute("age",25);
model.addAttribute("hello","hello world");
return "demo";
}
}
在resources中創(chuàng)建templates目錄杏慰,在templates目錄創(chuàng)建 demo.html,代碼如下:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Thymeleaf的入門</title>
</head>
<body>
<!--輸出hello數(shù)據(jù)-->
<p th:text="${hello}"></p>
</body>
</html>
解釋:
<html xmlns:th="http://www.thymeleaf.org"> :這句聲明使用thymeleaf標簽
<p th:text="${hello}"></p> :這句使用 th:text="${變量名}"
表示 使用thymeleaf獲取文本數(shù)據(jù)测柠,類似于EL表達式。
Thymeleaf基本語法
開胃小菜過后缘滥,現(xiàn)在來具體介紹基本語法
(1)th:action
定義后臺控制器路徑轰胁,類似 <form> 標簽的action屬性。
例如:
<form th:action="@{/test/hello}" >
<input th:type="text" th:name="id">
<button>提交</button>
</form>
(2)th:each
對象遍歷朝扼,功能類似jstl中的 <c:forEach> 標簽赃阀。
<table>
<tr>
<td>下標</td>
<td>編號</td>
<td>姓名</td>
<td>住址</td>
</tr>
<tr th:each="user,userStat:${userList}">
<td>
<span th:text="${userStat.index}"></span>
</td>
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.address}"></td>
</tr>
</table>
(3)Map輸出
<div th:each="map,mapStat:${dataMap}">
<div th:text="${map}"></div>
key:<span th:text="${mapStat.current.key}"></span><br/>
value:<span th:text="${mapStat.current.value}"></span><br/>
</div>
(4)數(shù)組輸出
<div th:each="nm,nmStat:${names}">
<span th:text="${nmStat.count}"></span>
<span th:text="${nm}"></span>
</div>
(5)Date輸出
<div>
<span th:text="${#dates.format(now,'yyyy-MM-dd hh:mm:ss')}"></span>
</div>
(6)th:if條件
<div>
<span th:if="${(age>=18)}">終于成年了</span>
</div>
(7)th:fragment 定義一個模塊
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>fragment</title>
</head>
<body>
<div id="C" th:fragment="copy">
關(guān)于我們<br/>
</div>
</body>
</html>
(8)th:include
可以直接引入 th:fragment
<div id="W" th:include="footer::copy"></div>
在這獻上幾張小圖圖,一鍵三連哦