SpringBoot與Thymeleaf入門級操作

来源:https://www.cnblogs.com/mysweetAngleBaby/archive/2022/09/10/16676670.html
-Advertisement-
Play Games

使用Thymeleaf 三大理由: 簡潔漂亮 容易理解 完美支持HTML5 使用瀏覽器直接打開頁面 不新增標簽 只需增強屬性 學習目標 快速掌握Thymeleaf的基本使用:五大基礎語法,常用內置對象 快速查閱 源碼下載:springboot-web-thymeleaf-enhance — Hey ...


使用Thymeleaf 三大理由:

  1. 簡潔漂亮 容易理解
  2. 完美支持HTML5 使用瀏覽器直接打開頁面
  3. 不新增標簽 只需增強屬性

學習目標

  • 快速掌握Thymeleaf的基本使用:五大基礎語法,常用內置對象

快速查閱

源碼下載:springboot-web-thymeleaf-enhance

— Hey Man,Don't forget to Star or Fork . —

官方指南: Thymleaf 3.0 官方教程

使用教程

溫馨提示:Thymeleaf 最為顯著的特征是增強屬性,任何屬性都可以通過th:xx 來完成交互,例如th:value最終會覆蓋value屬性。

一、基礎語法

變數表達式  ${}

使用方法:直接使用th:xx = "${}" 獲取對象屬性 。例如:

<form id="userForm">
    <input id="id" name="id" th:value="${user.id}"/>
    <input id="username" name="username" th:value="${user.username}"/>
    <input id="password" name="password" th:value="${user.password}"/>
</form>
<div th:text="hello"></div>
<div th:text="${user.username}"></div>

選擇變數表達式 *{}

使用方法:首先通過th:object 獲取對象,然後使用th:xx = "*{}"獲取對象屬性。

這種簡寫風格極為清爽,推薦大家在實際項目中使用。 例如:

<form id="userForm" th:object="${user}">
    <input id="id" name="id" th:value="*{id}"/>
    <input id="username" name="username" th:value="*{username}"/>
    <input id="password" name="password" th:value="*{password}"/>
</form>

鏈接表達式 @{}

使用方法:通過鏈接表達式@{}直接拿到應用路徑,然後拼接靜態資源路徑。例如:

<script th:src="@{/webjars/jquery/jquery.js}"></script>
<link th:href="@{/webjars/bootstrap/css/bootstrap.css}" rel="stylesheet" type="text/css">

片段表達式 ~{}

片段表達式是Thymeleaf的特色之一,細粒度可以達到標簽級別,這是JSP無法做到的。

片段表達式擁有三種語法:

  • ~{ viewName } 表示引入完整頁面
  • ~{ viewName ::selector} 表示在指定頁面尋找片段 其中selector可為片段名、jquery選擇器等
  • ~{ ::selector} 表示在當前頁尋找

使用方法:首先通過th:fragment定製片段 ,然後通過th:replace 填寫片段路徑和片段名。例如:

<!-- /views/common/head.html-->
<head th:fragment="static">
        <script th:src="@{/webjars/jquery/3.3.1/jquery.js}"></script>
</head>
<!-- /views/your.html -->
<div th:replace="~{common/head::static}"></div>

在實際使用中,我們往往使用更簡潔的表達,去掉表達式外殼直接填寫片段名。例如:

<!-- your.html -->
<div th:replace="common/head::static"></div>

值得註意的是,使用替換路徑th:replace 開頭請勿添加斜杠,避免部署運行的時候出現路徑報錯。(因為預設拼接的路徑為spring.thymeleaf.prefix = classpath:/templates/

消息表達式

即通常的國際化屬性:#{msg} 用於獲取國際化語言翻譯值。例如:

<title th:text="#{user.title}"></title>

其它表達式

在基礎語法中,預設支持字元串連接、數學運算、布爾邏輯和三目運算等。例如:

<input name="name" th:value="${'I am '+(user.name!=null?user.name:'NoBody')}"/>

二、內置對象

官方文檔: 附錄A: Thymeleaf 3.0 基礎對象

官方文檔: 附錄B: Thymeleaf 3.0 工具類 

七大基礎對象:

  • ${#ctx} 上下文對象,可用於獲取其它內置對象。
  • ${#vars}: 上下文變數。
  • ${#locale}:上下文區域設置。
  • ${#request}: HttpServletRequest對象。
  • ${#response}: HttpServletResponse對象。
  • ${#session}: HttpSession對象。
  • ${#servletContext}: ServletContext對象。

常用的工具類:

  • #strings:字元串工具類
  • #lists:List 工具類
  • #arrays:數組工具類
  • #sets:Set 工具類
  • #maps:常用Map方法。
  • #objects:一般對象類,通常用來判斷非空
  • #bools:常用的布爾方法。
  • #execInfo:獲取頁面模板的處理信息。
  • #messages:在變數表達式中獲取外部消息的方法,與使用#{...}語法獲取的方法相同。
  • #uris:轉義部分URL / URI的方法。
  • #conversions:用於執行已配置的轉換服務的方法。
  • #dates:時間操作和時間格式化等。
  • #calendars:用於更複雜時間的格式化。
  • #numbers:格式化數字對象的方法。
  • #aggregates:在數組或集合上創建聚合的方法。
  • #ids:處理可能重覆的id屬性的方法。

三、迭代迴圈

想要遍歷List集合很簡單,配合th:each 即可快速完成迭代。例如遍歷用戶列表:

<div th:each="user:${userList}">
    賬號:<input th:value="${user.username}"/>
    密碼:<input th:value="${user.password}"/>
</div>

在集合的迭代過程還可以獲取狀態變數,只需在變數後面指定狀態變數名即可,狀態變數可用於獲取集合的下標/序號、總數、是否為單數/偶數行、是否為第一個/最後一個。例如:

<div th:each="user,stat:${userList}" th:class="${stat.even}?'even':'odd'">
    下標:<input th:value="${stat.index}"/>
    序號:<input th:value="${stat.count}"/>
    賬號:<input th:value="${user.username}"/>
    密碼:<input th:value="${user.password}"/>
</div>

如果預設狀態變數名,則迭代器會預設幫我們生成以變數名開頭的狀態變數 xxStat, 例如:

<div th:each="user:${userList}" th:class="${userStat.even}?'even':'odd'">
    下標:<input th:value="${userStat.index}"/>
    序號:<input th:value="${userStat.count}"/>
    賬號:<input th:value="${user.username}"/>
    密碼:<input th:value="${user.password}"/>
</div>

四、條件判斷
條件判斷通常用於動態頁面的初始化,例如:

<div th:if="${userList}">
    <div>的確存在..</div>
</div>

如果想取反則使用unless 例如:

<div th:unless="${userList}">
    <div>不存在..</div>
</div>

五、日期格式化

使用預設的日期格式(toString方法) 並不是我們預期的格式:Mon Dec 03 23:16:50 CST 2018

<input type="text" th:value="${user.createTime}"/>

此時可以通過時間工具類#dates來對日期進行格式化:2018-12-03 23:16:50

<input type="text" th:value="${#dates.format(user.createTime,'yyyy-MM-dd HH:mm:ss')}"/>

六、內聯寫法

  • (1)為什麼要使用內聯寫法?·答:因為 JS無法獲取服務端返回的變數。
  • (2)如何使用內聯表達式?答:標準格式為:[[${xx}]] ,可以讀取服務端變數,也可以調用內置對象的方法。例如獲取用戶變數和應用路徑:
<script th:inline="javascript">
  var user = [[${user}]];`
  var APP_PATH = [[${#request.getContextPath()}]];
  var LANG_COUNTRY = [[${#locale.getLanguage()+'_'+#locale.getCountry()}]];
</script>
  • (3)標簽引入的JS裡面能使用內聯表達式嗎?答:不能!內聯表達式僅在頁面生效,因為Thymeleaf只負責解析一級視圖,不能識別外部標簽JS裡面的表達式。

七、國際化

需要瞭解更多關於國際化的精彩描述請前往 SpringBoot 快速實現國際化i18n 。

例如在國際化文件中編寫了user.title這個鍵值,然後使用#{}讀取這個KEY即可獲取翻譯。

<title th:text="#{user.title}">用戶登陸</title>

八、詳細教程

======== 有了上述基礎後 下麵正式開始Thymeleaf 的詳細教程 ==============

首先通過Spring Initializr創建項目,

然後在POM文件引入web 、thymeleaf等依賴

<dependencies>
        <dependency><!--Web相關依賴-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency><!--頁面模板依賴-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency><!--熱部署依賴-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

然後在src/main/resources/application.yml 配置頁面路徑:

spring:
  thymeleaf:
    cache: false #關閉緩存
    prefix: classpath:/views/ #調整頁面路徑

接著在src/main/java/com/hehe/web/UserController 獲取用戶信息:

@RestController
public class UserController {
    private List<User> userList = new ArrayList<>();
    {
        userList.add(new User("1", "socks", "123456", new Date()));
        userList.add(new User("2", "admin", "111111", new Date()));
        userList.add(new User("3", "jacks", "222222", null));
    }
    @GetMapping("/")
    public ModelAndView index() {
        return new ModelAndView("user/user", "userList", userList);
    }
}
public class User {
    private String id;
    private String username;
    private String password;
    private Date createTime;
    //請讀者自行補充 構造器和 get/set方法..
}

開始編寫公共頁面:src/main/resources/views/common/head.html ,其中static為頁面片段:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<!--聲明static為頁面片段名稱-->
<head th:fragment="static">
    <link th:href="@{/webjars/bootstrap/css/bootstrap.css}" rel="stylesheet" type="text/css"/>
    <script th:src="@{/webjars/jquery/jquery.js}"></script>
</head>
</html>

接著編寫用戶列表頁:src/main/resources/views/user/user.html 配合th:each顯示用戶列表信息。

使用說明:這裡 th:replace="common/head::static" 表示將引用${spring.thymeleaf.prefix}/common/head.htmlstatic頁面片段,值得註意的是由於替換路徑預設會拼接首碼路徑,所以開頭切勿在添加斜杠,否則在打包成JAR部署運行時將提示報Templates not found... 。

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title th:text="用戶信息">User</title>
    <!--預設拼接首碼路徑,開頭請勿再添加斜杠,防止部署運行報錯!-->
    <script th:replace="common/head::static"></script>
</head>
<body>
<div th:each="user,userStat:${userList}" th:class="${userStat.even}?'even':'odd'">
    序號:<input type="text" th:value="${userStat.count}"/>
    賬號:<input type="text" th:value="${user.username}"/>
    密碼:<input type="password" th:value="${user.password}"/>
    時間:<input type="text" th:value="${user.createTime}"/>
    時間:<input type="text" th:value="${#dates.format(user.createTime,'yyyy-MM-dd HH:mm:ss')}"/>
</div>
<script th:inline="javascript">
    //通過內聯表達式獲取用戶信息
    var userList = [[${userList}]];
    console.log(userList)
</script>
</body>
</html>

然後編寫單個用戶頁面:


至此大功告成,然後快速啟動項目,如圖所示:

1

快速啟動項目

然後訪問用戶列表: http://localhost:8080,如圖所示

1

然後訪問單個用戶: http://localhost:8080/user/1,如圖所示:

1


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一個菜鳥的設計模式之旅,本程式實現觀察者模式。使用C#、Go兩門語言分別進行實現。程式創建一個全局游戲死亡事件通知,5個玩家、1個Boss,當任意一方死亡時,在場存活者都能收到陣亡者的消息。 ...
  • 第一步檢查環境變數 打開cmd 查看以下環境變數 需要軟體: Typora PicGo gitee賬號 配置node 配置git 第二步創建gitee倉庫 設置倉庫名直接創建,因為這裡不能直接修改開源,需要在設置修改 找到剛剛創建的倉庫 點擊管理往下拉設置開源 然後點擊設置私人令牌 生成新令牌 寫個 ...
  • 2022-09-10 閉包的使用實例 1 def config_name(name): 2 def inner(msg): 3 print(name + ":" + msg) 4 5 print(id(inner)) 6 return inner 7 8 A = config_name("A") 9 ...
  • JavaOI流02 4.常用的類 4.1文件位元組流輸入流-FileInputStream InputStream抽象類是所有類位元組輸入流的超類 InputStream常用的子類: FileInputStream:文件位元組輸入流 BufferedInputStream:緩衝位元組輸入流 ObjectIn ...
  • OpenFeign攔截器 在微服務中比較常見的場景:前端帶了JWT令牌請求服務A,在服務A中使用Feign遠程調用服務B、服務C等,A、B、C都接入了Spring Security;此時就會存在這樣的需求,如服務A調用服務B、C時不帶有JWT令牌就會出現服務調用失敗,無法通過服務B、C鑒權認證; 此 ...
  • 基礎環境 centos7 安裝BT寶塔 網址:https://www.bt.cn/download/linux.html 安裝ORACLE客戶端 下載地址: https://www.oracle.com/database/technologies/instant-client/linux-x86-6 ...
  • SpringMVC(精簡) 一、SpringMVC介紹 1.什麼是MVC 是一種軟體架構的思想,將軟體按照模型、視圖、控制器來劃分 **M: **Model,模型層,指工程中的JavaBean,作用是處理數據 JavaBean 一類稱為實體類Bean:專門存儲業務數據的,如 Student、User ...
  • 這幾天想搞到一個三階魔方排行榜的數據,官網居然不能導出Excel文件,剛好這幾天學了個爬蟲,於是爬著玩玩(應該不會進去)。 1 目標網站: https://www.worldcubeassociation.org/results/rankings/333/average 2 準備庫 ## 準備的庫 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...