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ī)則,我們進行使用??梢韵热タ纯碩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)文章
關(guān)于Assert.assertEquals報錯的問題及解決
這篇文章主要介紹了關(guān)于Assert.assertEquals報錯的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05JavaWeb 中Cookie實現(xiàn)記住密碼的功能示例
cookie是一種WEB服務(wù)器通過瀏覽器在訪問者的硬盤上存儲信息的手段。Cookie的目的就是為用戶帶來方便,為網(wǎng)站帶來增值。這篇文章主要介紹了JavaWeb 中Cookie實現(xiàn)記住密碼的功能示例,需要的朋友可以參考下2017-06-06Java 注冊時發(fā)送激活郵件和激活的實現(xiàn)示例
這篇文章主要介紹了Java 注冊時發(fā)送激活郵件和激活的實現(xiàn)示例的相關(guān)資料,需要的朋友可以參考下2017-07-07