Thymeleaf是一個(gè)XML/XHTML/HTML5模板引擎这难,可用于Web與非Web環(huán)境中的應(yīng)用開發(fā)叉庐。它是一個(gè)開源的Java庫种樱,基于Apache License 2.0許可萧求,由Daniel Fernández創(chuàng)建托享,該作者還是Java加密庫Jasypt的作者骚烧。
Thymeleaf提供了一個(gè)用于整合Spring MVC的可選模塊浸赫,在應(yīng)用開發(fā)中和橙,你可以使用Thymeleaf來完全代替JSP乓土,或其他模板引擎囱井,如Velocity赃磨、FreeMarker等鳞疲。Thymeleaf的主要目標(biāo)在于提供一種可被瀏覽器正確顯示的临庇、格式良好的模板創(chuàng)建方式叼风,因此也可以用作靜態(tài)建模朋譬。你可以使用它創(chuàng)建經(jīng)過驗(yàn)證的XML與HTML模板么夫。相對(duì)于編寫邏輯或代碼者冤,開發(fā)者只需將標(biāo)簽屬性添加到模板中即可。接下來档痪,這些標(biāo)簽屬性就會(huì)在DOM(文檔對(duì)象模型)上執(zhí)行預(yù)先制定好的邏輯涉枫。
1、在項(xiàng)目中添加引用
1腐螟、maven的引用方式:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2愿汰、gradle的引用方式:
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
2、在controller編寫代碼乐纸,返回視圖
@RequestMapping("/index")
public String index(ModelMap map) { // 加入一個(gè)屬性衬廷,用來在模板中讀取
map.addAttribute("host", "http://www.bilibili.com");
// return模板文件的名稱,對(duì)應(yīng)src/main/resources/templates/index.html
return "index";
}
3汽绢、index.html
<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<h1 th:text="${host}">Hello World</h1>
</body>
</html>
4吗跋、運(yùn)行效果
5、其他
# 代表 獲取對(duì)象 從 messages bundle 也就是消息的資源本地化文件
$ 表示從model里面獲取
th:fragment=“public” 相當(dāng)于 include標(biāo)簽
th:each="user : ${users}" 相當(dāng)于c:foreach
如:
<tr th:each="user : ${users}">
<td th:text="${user.id}">2333333</td>
<td th:text="${user.name}">紅紅火火恍恍惚惚</td>
</tr>
th:text表示動(dòng)態(tài)設(shè)置屬性的值
th:action用于表單的action
th:if用作if判斷
內(nèi)聯(lián)js:
<script th:inline="javascript">
/*<![CDATA[*/
...var username = /*[[${sesion.user.name}]]*/ 'Sebastian';...
/*]]>*/
</script>
js附加代碼:
/*[+
var msg = 'This is a working application';
+]*/
js移除代碼:
/*[- */
var msg = 'This is a non-working template';
/* -]*/
先就這么多吧~~