SpringBoot引入Thymeleaf的實(shí)現(xiàn)方法
1.Thymeleaf簡(jiǎn)介
Thymeleaf是個(gè)XML/XHTML/HTML5模板引擎,可以用于Web與非Web應(yīng)用
Thymeleaf的主要目標(biāo)在于提供一種可被瀏覽器正確顯示的、格式良好的模板創(chuàng)建方式,因此也可以用作靜態(tài)建模,Thymeleaf的可擴(kuò)展性也非常棒。你可以使用它定義自己的模板屬性集合,這樣就可以計(jì)算自定義表達(dá)式并使用自定義邏輯,Thymeleaf還可以作為模板引擎框架。
2.引入Thymeleaf
引入依賴
在maven(pom.xml)中直接引入:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
配置Thymeleaf
在application.yml配置Thymeleaf
server: port: 8000 spring: thymeleaf: cache: false # 關(guān)閉頁面緩存 encoding: UTF-8 # 模板編碼 prefix: classpath:/templates/ # 頁面映射路徑 suffix: .html # 試圖后的后綴 mode: HTML5 # 模板模式 # 其他具體配置可參考o(jì)rg.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties # 上面的配置實(shí)際上就是注入該類的屬性值
demo示例
創(chuàng)建IndexController
@Controller
public class IndexController {
// 返回視圖頁面
@RequestMapping("index")
public String index(){
return "index";
}
}
創(chuàng)建index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> Hello Thymeleaf! </body> </html>
創(chuàng)建TestController
@RestController
public class TestController {
// 返回整個(gè)頁面
@RequestMapping("/test")
public ModelAndView test(){
return new ModelAndView("test");
}
}
創(chuàng)建test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> Hello Thymeleaf! </br> By: ModelAndView </body> </html>
3.測(cè)試結(jié)果


4.Thymeleaf基礎(chǔ)語法及使用
1.引入標(biāo)簽
html標(biāo)簽里引入xmlns:th="http://www.thymeleaf.org"才能使用th:*這樣的語法
2.引入U(xiǎn)RL
@{...}
例如:
<a th:href="@{http://www.baidu.com}" rel="external nofollow" >絕對(duì)路徑</a> 是訪問絕對(duì)路徑下的URL, <a th:href="@{/}" rel="external nofollow" >相對(duì)路徑</a> 是訪問相對(duì)路徑下的URL。
<a th:href="@{css/bootstrap.min.css}" rel="external nofollow" >是引入默認(rèn)的static下的css文件夾下的bootstrap文件,類似的標(biāo)簽有: th:href 和 th:src
3.獲取變量
通過${}取值,對(duì)于JavaBean的話,使用變量名.屬性名獲取
4.字符串替換
<span th:text="'Welcome to our application, ' + ${user.name} + '!'"></span>
或者
<span th:text="|Welcome to our application, ${user.name}!|"></span>
注意:|…|中只能包含變量表達(dá)式${…},不能包含其他常量、條件表達(dá)式等
5.運(yùn)算符
在表達(dá)式中可以使用各類算術(shù)運(yùn)算符
例如 (+, -, *, /, %)
例如:th:with="isEven=(${stat.number} % 1 == 0)"
邏輯運(yùn)算符 (>, <, <=,>=,==,!=)
需要注意的是使用<,>的時(shí)候需要轉(zhuǎn)義
th:if="${stat.number} > 1"
th:text="'Execution mode is ' + ( (${execMode} == 'dev')? 'Development' : 'Production')"
6.條件
if/unless th:if是該標(biāo)簽在滿足條件的時(shí)候才會(huì)顯示,unless是不成立時(shí)候才顯示
<a th:href="@{/login}" rel="external nofollow" th:unless=${user.number != null}>Login</a>
switch thymeleaf支持switch結(jié)構(gòu),默認(rèn)屬性(default)用*表示
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
<p th:case="*">User is some other thing</p>
</div>
7.循環(huán)
<tr th:each="prod : ${prods}">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
8.Utilities
內(nèi)置在Context中,可以直接通過#訪問
#dates
#calendars
#numbers
#strings
arrays
lists
sets
maps
…
5.小結(jié)
本文講述了如何在Spring Boot中引入模板引擎Thymeleaf以及Thymeleaf基礎(chǔ)語法和實(shí)際使用
本文GitHub地址:https://github.com/ishuibo/SpringAll
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot使用thymeleaf實(shí)現(xiàn)前端表格
- SpringBoot使用thymeleaf實(shí)現(xiàn)一個(gè)前端表格方法詳解
- SpringBoot+thymeleaf+ajax實(shí)現(xiàn)局部刷新詳情
- 在SpringBoot中配置Thymeleaf的模板路徑方式
- springboot+thymeleaf打包成jar后找不到靜態(tài)資源的坑及解決
- springboot用thymeleaf模板的paginate分頁完整代碼
- springboot中thymeleaf模板使用詳解
- springboot 中 thymeleaf 常用的語法完整實(shí)例
相關(guān)文章
劍指Offer之Java算法習(xí)題精講鏈表與字符串及數(shù)組
跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03
mybatis中關(guān)于type-aliases-package的使用
這篇文章主要介紹了mybatis中關(guān)于type-aliases-package的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
IDEA 單元測(cè)試創(chuàng)建方法詳解(2020.03版本親測(cè))
這篇文章主要介紹了IDEA 單元測(cè)試創(chuàng)建方法詳解(2020.03版本親測(cè)),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Spring MVC獲取查詢參數(shù)及路徑參數(shù)代碼實(shí)例
這篇文章主要介紹了Spring MVC獲取查詢參數(shù)及路徑參數(shù)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
詳解java中反射機(jī)制(含數(shù)組參數(shù))
這篇文章主要介紹了詳解java中反射機(jī)制(含數(shù)組參數(shù))的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10
SpringMVC中控制器返回JSON數(shù)據(jù)的實(shí)現(xiàn)
本文主要介紹了SpringMVC中控制器返回JSON數(shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
java的三種隨機(jī)數(shù)生成方式的實(shí)現(xiàn)方法
這篇文章主要介紹了java的三種隨機(jī)數(shù)生成方式的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
SpringMVC攔截器實(shí)現(xiàn)單點(diǎn)登錄
這篇文章主要介紹了SpringMVC攔截器實(shí)現(xiàn)單點(diǎn)登錄,簡(jiǎn)單介紹了springmvc攔截器,單點(diǎn)登錄實(shí)現(xiàn)原理等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11

