Java SpringBoot模板引擎之 Thymeleaf入門(mén)詳解
模板引擎簡(jiǎn)介
如果我們直接用純靜態(tài)頁(yè)面方式,必然會(huì)給開(kāi)發(fā)帶來(lái)很大麻煩,所以springboot推薦使用模板引擎,其實(shí)jsp就是一個(gè)模板引擎,還有用的比較多的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自動(dòng)下載jar包,下圖試maven下載的東西;

分析Thymeleaf模板引擎
首先按照SpringBoot的自動(dòng)配置原理來(lái)看一下我們這個(gè)Thymeleaf的自動(dòng)配置規(guī)則,再按照這個(gè)規(guī)則,我們進(jìn)行使用。可以先去看看Thymeleaf的自動(dòng)配置類(lèi):ThymeleafProperties

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

2、編寫(xiě)一個(gè)測(cè)試頁(yè)面 test.html 放在 templates 目錄下

3、啟動(dòng)項(xiàng)目請(qǐng)求測(cè)試

4、結(jié)論
只要需要使用thymeleaf,只需要導(dǎo)入對(duì)應(yīng)的依賴就可以了,然后將html放在templates的目錄下即可
Thymeleaf入門(mén):
我們可以查看下Thymeleaf 官網(wǎng):https://www.thymeleaf.org/
簡(jiǎn)單練習(xí):查出一些數(shù)據(jù),在頁(yè)面中展示
1、修改測(cè)試請(qǐng)求,增加數(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)ゾ帉?xiě)下前端頁(yè)面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>九陽(yáng)真經(jīng)---龍弟</title>
</head>
<body>
<h1>測(cè)試頁(yè)面</h1>
<!--th:text就是將div中的內(nèi)容設(shè)置為它指定的值-->
<div th:text="${msg}"></div>
</body>
</html>
4、啟動(dòng)測(cè)試!

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

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

練習(xí)測(cè)試
@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、測(cè)試頁(yè)面取出數(shù)據(jù)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>九陽(yáng)真經(jīng)---龍弟</title>
</head>
<body>
<h1>測(cè)試頁(yè)面</h1>
<div th:text="${msg}"></div>
<!--不轉(zhuǎn)義-->
<div th:utext="${msg}"></div>
<!--遍歷數(shù)據(jù)-->
<!--th:each每次遍歷都會(huì)生成當(dāng)前這個(gè)標(biāo)簽-->
<h4 th:each="user :${users}" th:text="${user}"></h4>
<hr>
<!--行內(nèi)寫(xiě)法-->
<h4 th:each="user:${users}">[[${user}]]</h4>
</body>
</html>
3、啟動(dòng)項(xiàng)目測(cè)試!

總結(jié):
由于thymeleaf很多語(yǔ)法樣式,我們現(xiàn)在學(xué)了也會(huì)忘記,因此,在學(xué)習(xí)過(guò)程中,需要使用什么,根據(jù)官方文檔來(lái)查詢,所以要熟練使用官方文檔!
到此這篇關(guān)于Java SpringBoot模板引擎之 Thymeleaf入門(mén)詳解的文章就介紹到這了,更多相關(guān)Java SpringBoot Thymeleaf內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot 動(dòng)態(tài)數(shù)據(jù)源的實(shí)現(xiàn)方法(Mybatis+Druid)
這篇文章主要介紹了springboot 動(dòng)態(tài)數(shù)據(jù)源的實(shí)現(xiàn)方法(Mybatis+Druid),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Java ThreadLocal原理解析以及應(yīng)用場(chǎng)景分析案例詳解
這篇文章主要介紹了Java ThreadLocal原理解析以及應(yīng)用場(chǎng)景分析案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
解決springboot項(xiàng)目找不到resources目錄下的資源問(wèn)題
這篇文章主要介紹了解決springboot項(xiàng)目找不到resources目錄下的資源問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

