springboot 中 thymeleaf 常用的語法完整實例
在 Spring Boot 項目中,Thymeleaf 是一個常用的模板引擎,它提供了豐富的語法來動態(tài)渲染 HTML 頁面。以下是一些常用的 Thymeleaf 語法,以及一個完整的 Spring Boot + Thymeleaf 示例。

Thymeleaf 常用語法
表達式:
<!-- 獲取變量值 -->
<p th:text="${message}">默認消息</p>
<!-- 獲取對象的屬性 -->
<p th:text="${user.name}">默認用戶名</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>完整代碼
以下是一個完整的 Spring Boot + Thymeleaf 示例,包括一個簡單的控制器和一個 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}">默認消息</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}">默認消息</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 提供了豐富的語法來動態(tài)渲染 HTML 頁面,包括表達式、選擇、鏈接、屬性、片段和內(nèi)容等。通過這些語法,你可以輕松地在 Spring Boot 應(yīng)用中實現(xiàn)數(shù)據(jù)驅(qū)動的頁面渲染。在實際開發(fā)中,你可以根據(jù)項目需求靈活運用這些語法,以創(chuàng)建功能豐富且易于維護的 Web 應(yīng)用。
以上內(nèi)容涵蓋了 Thymeleaf 的基本語法和示例代碼,希望這能幫助你了解如何在 Spring Boot 項目中使用 Thymeleaf。
到此這篇關(guān)于springboot 中 thymeleaf 常用的語法的文章就介紹到這了,更多相關(guān)springboot thymeleaf 語法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實現(xiàn)一個接口調(diào)取另一個接口(接口一調(diào)取接口二)
這篇文章主要介紹了java實現(xiàn)一個接口調(diào)取另一個接口(接口一調(diào)取接口二),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
SpringBoot詳細講解通過自定義classloader加密保護class文件
這篇文章主要介紹了SpringBoot通過自定義classloader加密class文件,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
如何優(yōu)雅的實現(xiàn)將Collection轉(zhuǎn)為Map
這篇文章主要介紹了如何優(yōu)雅的實現(xiàn)將Collection轉(zhuǎn)為Map,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
springboot自動掃描添加的BeanDefinition源碼實例詳解
這篇文章主要給大家介紹了關(guān)于springboot自動掃描添加的BeanDefinition的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-02-02

