Java SpringBoot模板引擎之 Thymeleaf入門詳解
模板引擎簡介
如果我們直接用純靜態(tài)頁面方式,必然會給開發(fā)帶來很大麻煩,所以springboot推薦使用模板引擎,其實jsp就是一個模板引擎,還有用的比較多的freemarker,包括SpringBoot給我們推薦的Thymeleaf!模板引擎的本質(zhì)思想如下圖:

引入Thymeleaf模板引擎
Thymeleaf 官網(wǎng):Thymeleaf

Spring官方文檔:
https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Maven自動下載jar包,下圖試maven下載的東西;

分析Thymeleaf模板引擎
首先按照SpringBoot的自動配置原理來看一下我們這個Thymeleaf的自動配置規(guī)則,再按照這個規(guī)則,我們進(jìn)行使用??梢韵热タ纯碩hymeleaf的自動配置類:ThymeleafProperties

我們可以在配置文件看到默認(rèn)的前綴和后綴!
我們只需要把我們的html頁面放在類路徑下的templates下,thymeleaf就可以幫我們自動渲染。
測試Thymeleaf模板引擎
1、編寫一個TestController

2、編寫一個測試頁面 test.html 放在 templates 目錄下

3、啟動項目請求測試

4、結(jié)論
只要需要使用thymeleaf,只需要導(dǎo)入對應(yīng)的依賴就可以了,然后將html放在templates的目錄下即可
Thymeleaf入門:
我們可以查看下Thymeleaf 官網(wǎng):https://www.thymeleaf.org/
簡單練習(xí):查出一些數(shù)據(jù),在頁面中展示
1、修改測試請求,增加數(shù)據(jù)傳輸
@Controller
public class TestController {
@RequestMapping("/t1")
public String test1(Model model){
//存入數(shù)據(jù)
model.addAttribute("msg","Hello,Thymeleaf");
//classpath:/templates/test.html
return "test";
}
}
2、使用thymeleaf
我們要使用thymeleaf,需要在html文件中導(dǎo)入命名空間的約束,方便提示。
xmlns:th=http://www.thymeleaf.org
3、我們?nèi)ゾ帉懴虑岸隧撁?/h3>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>九陽真經(jīng)---龍弟</title>
</head>
<body>
<h1>測試頁面</h1>
<!--th:text就是將div中的內(nèi)容設(shè)置為它指定的值-->
<div th:text="${msg}"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>九陽真經(jīng)---龍弟</title>
</head>
<body>
<h1>測試頁面</h1>
<!--th:text就是將div中的內(nèi)容設(shè)置為它指定的值-->
<div th:text="${msg}"></div>
</body>
</html>
4、啟動測試!

thymeleaf語法學(xué)習(xí)
1、使用任意的 th:attr 來替換Html中原生屬性的值!

2、表達(dá)式語法:

練習(xí)測試
@Controller
public class TestController {
@RequestMapping("/t2")
public String test2(Map<String,Object> map){
//存入數(shù)據(jù)
map.put("msg","<h1>Hello,SpringBoot</h1>");
map.put("users", Arrays.asList("dragon","longdi"));
//classpath:/templates/test.html
return "test";
}
}
2、測試頁面取出數(shù)據(jù)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>九陽真經(jīng)---龍弟</title>
</head>
<body>
<h1>測試頁面</h1>
<div th:text="${msg}"></div>
<!--不轉(zhuǎn)義-->
<div th:utext="${msg}"></div>
<!--遍歷數(shù)據(jù)-->
<!--th:each每次遍歷都會生成當(dāng)前這個標(biāo)簽-->
<h4 th:each="user :${users}" th:text="${user}"></h4>
<hr>
<!--行內(nèi)寫法-->
<h4 th:each="user:${users}">[[${user}]]</h4>
</body>
</html>
3、啟動項目測試!

總結(jié):
由于thymeleaf很多語法樣式,我們現(xiàn)在學(xué)了也會忘記,因此,在學(xué)習(xí)過程中,需要使用什么,根據(jù)官方文檔來查詢,所以要熟練使用官方文檔!
到此這篇關(guān)于Java SpringBoot模板引擎之 Thymeleaf入門詳解的文章就介紹到這了,更多相關(guān)Java SpringBoot Thymeleaf內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot 動態(tài)數(shù)據(jù)源的實現(xiàn)方法(Mybatis+Druid)
Java ThreadLocal原理解析以及應(yīng)用場景分析案例詳解
解決springboot項目找不到resources目錄下的資源問題

