springboot 中 thymeleaf 常用的語法完整實(shí)例
在 Spring Boot 項(xiàng)目中,Thymeleaf 是一個(gè)常用的模板引擎,它提供了豐富的語法來動(dòng)態(tài)渲染 HTML 頁面。以下是一些常用的 Thymeleaf 語法,以及一個(gè)完整的 Spring Boot + Thymeleaf 示例。
Thymeleaf 常用語法
表達(dá)式:
<!-- 獲取變量值 --> <p th:text="${message}">默認(rèn)消息</p> <!-- 獲取對(duì)象的屬性 --> <p th:text="${user.name}">默認(rèn)用戶名</p>
選擇:
<!-- 判斷 --> <p th:if="${user.active}">用戶激活</p> <!-- 選擇 --> <div th:switch="${user.role}"> <p th:case="'admin'">管理員</p> <p th:case="'user'">普通用戶</p> </div>
鏈接:
<!-- 跳轉(zhuǎn)到指定 URL --> <a th:href="@{/profile}" rel="external nofollow" >查看資料</a> <!-- 帶參數(shù)的 URL --> <a th:href="@{/profile(id=${user.id})}" rel="external nofollow" rel="external nofollow" >查看資料</a>
屬性:
<!-- 替換屬性 --> <div th:attr="id=${user.id}, class=${user.active ? 'active' : 'inactive'}">用戶信息</div>
片段:
<!-- 引入片段 --> <div th:insert="~{commons :: header}">頁面頭部</div> <!-- 替換片段 --> <div th:replace="~{commons :: header}">頁面頭部</div>
內(nèi)容:
<!-- 插入內(nèi)容 --> <div th:insert="~{commons :: content"> <!-- 頁面內(nèi)容 --> </div> <!-- 替換內(nèi)容 --> <div th:replace="~{commons :: content"> <!-- 頁面內(nèi)容 --> </div>
完整代碼
以下是一個(gè)完整的 Spring Boot + Thymeleaf 示例,包括一個(gè)簡單的控制器和一個(gè) Thymeleaf 模板文件。
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- 其他依賴 --> </dependencies>
application.properties
spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.cache=false
ThymeleafController.java
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class ThymeleafController { @GetMapping("/") public String index(Model model) { model.addAttribute("message", "歡迎來到 Spring Boot + Thymeleaf 示例!"); return "index"; } }
index.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Spring Boot + Thymeleaf 示例</title> </head> <body> <h1 th:text="${message}">默認(rèn)消息</h1> </body> </html>
application.properties
properties
spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.cache=false
index.html (繼續(xù))
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Spring Boot + Thymeleaf 示例</title> </head> <body> <h1 th:text="${message}">默認(rèn)消息</h1> <p th:if="${user.active}">用戶已激活</p> <p th:unless="${user.active}">用戶未激活</p> <div th:switch="${user.role}"> <p th:case="'admin'">管理員</p> <p th:case="'user'">普通用戶</p> <p th:case="*">未知角色</p> </div> <a th:href="@{/profile(id=${user.id})}" rel="external nofollow" rel="external nofollow" >查看資料</a> <div th:insert="~{commons :: header}">頁面頭部</div> <div th:replace="~{commons :: content}">頁面內(nèi)容</div> </body> </html>
Commons.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>公共片段</title> </head> <body> <div th:fragment="header">頁面頭部</div> <div th:fragment="content">頁面內(nèi)容</div> </body> </html>
總結(jié)
Thymeleaf 提供了豐富的語法來動(dòng)態(tài)渲染 HTML 頁面,包括表達(dá)式、選擇、鏈接、屬性、片段和內(nèi)容等。通過這些語法,你可以輕松地在 Spring Boot 應(yīng)用中實(shí)現(xiàn)數(shù)據(jù)驅(qū)動(dòng)的頁面渲染。在實(shí)際開發(fā)中,你可以根據(jù)項(xiàng)目需求靈活運(yùn)用這些語法,以創(chuàng)建功能豐富且易于維護(hù)的 Web 應(yīng)用。
以上內(nèi)容涵蓋了 Thymeleaf 的基本語法和示例代碼,希望這能幫助你了解如何在 Spring Boot 項(xiàng)目中使用 Thymeleaf。
到此這篇關(guān)于springboot 中 thymeleaf 常用的語法的文章就介紹到這了,更多相關(guān)springboot thymeleaf 語法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 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的實(shí)現(xiàn)方法
- springboot用thymeleaf模板的paginate分頁完整代碼
- springboot中thymeleaf模板使用詳解
相關(guān)文章
JAVAEE中用Session簡單實(shí)現(xiàn)購物車功能示例代碼
本篇文章主要介紹了JAVAEE中用Session簡單實(shí)現(xiàn)購物車功能示例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03SpringMVC高級(jí)開發(fā)功能實(shí)現(xiàn)過程解析
這篇文章主要介紹了SpringMVC高級(jí)開發(fā)功能實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06IDEA中的yml文件與properties互相轉(zhuǎn)換
這篇文章主要介紹了IDEA中的yml文件與properties互相轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10spring-boot-maven-plugin報(bào)紅解決方案(親測有效)
本文主要介紹了spring-boot-maven-plugin報(bào)紅解決方案,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03在SpringBoot中,如何使用Netty實(shí)現(xiàn)遠(yuǎn)程調(diào)用方法總結(jié)
我們?cè)谶M(jìn)行網(wǎng)絡(luò)連接的時(shí)候,建立套接字連接是一個(gè)非常消耗性能的事情,特別是在分布式的情況下,用線程池去保持多個(gè)客戶端連接,是一種非常消耗線程的行為.那么我們?cè)撏ㄟ^什么技術(shù)去解決上述的問題呢,那么就不得不提一個(gè)網(wǎng)絡(luò)連接的利器——Netty,需要的朋友可以參考下2021-06-06SpringBoot 開發(fā)提速神器 Lombok+MybatisPlus+SwaggerUI
這篇文章主要介紹了SpringBoot 開發(fā)提速神器 Lombok+MybatisPlus+SwaggerUI,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03