日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

【模板引擎】Springboot整合ThymeleafThymeleaf基本语法

發(fā)布時(shí)間:2025/5/22 71 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【模板引擎】Springboot整合ThymeleafThymeleaf基本语法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Thymeleaf介紹

thymeleaf是一個(gè)XML/XHTML/HTML5模板引擎,可用于Web與非Web環(huán)境中的應(yīng)用開發(fā)。它是一個(gè)開源的Java庫(kù),基于Apache License 2.0許可,由Daniel Fernández創(chuàng)建,該作者還是Java加密庫(kù)Jasypt的作者。
Thymeleaf提供了一個(gè)用于整合Spring MVC的可選模塊,在應(yīng)用開發(fā)中,你可以使用Thymeleaf來(lái)完全代替JSP或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目標(biāo)在于提供一種可被瀏覽器正確顯示的、格式良好的模板創(chuàng)建方式,因此也可以用作靜態(tài)建模。你可以使用它創(chuàng)建經(jīng)過(guò)驗(yàn)證的XML與HTML模板。相對(duì)于編寫邏輯或代碼,開發(fā)者只需將標(biāo)簽屬性添加到模板中即可。接下來(lái),這些標(biāo)簽屬性就會(huì)在DOM(文檔對(duì)象模型)上執(zhí)行預(yù)先制定好的邏輯。

它的特點(diǎn)便是:開箱即用,Thymeleaf允許您處理六種模板,每種模板稱為模板模式:
* XML
* 有效的XML
* XHTML
* 有效的XHTML
* HTML5
* 舊版HTML5
所有這些模式都指的是格式良好的XML文件,但Legacy HTML5模式除外,它允許您處理HTML5文件,其中包含獨(dú)立(非關(guān)閉)標(biāo)記,沒(méi)有值的標(biāo)記屬性或不在引號(hào)之間寫入的標(biāo)記屬性。為了在這種特定模式下處理文件,Thymeleaf將首先執(zhí)行轉(zhuǎn)換,將您的文件轉(zhuǎn)換為格式良好的XML文件,這些文件仍然是完全有效的HTML5(實(shí)際上是創(chuàng)建HTML5代碼的推薦方法)。

另請(qǐng)注意,驗(yàn)證僅適用于XML和XHTML模板。
然而,這些并不是Thymeleaf可以處理的唯一模板類型,并且用戶始終能夠通過(guò)指定在此模式下解析模板的方法和編寫結(jié)果的方式來(lái)定義他/她自己的模式。這樣,任何可以建模為DOM樹(無(wú)論是否為XML)的東西都可以被Thymeleaf有效地作為模板處理。

Springboot整合thymeleaf

使用springboot 來(lái)集成使用Thymeleaf可以大大減少單純使用thymleaf的代碼量
pom.xml依賴

<!-- 版本控制 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.4.RELEASE</version></parent><dependencies><!--web起步依賴--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--thymeleaf配置--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency></dependencies>

啟動(dòng)類ThymeleafApplication

@SpringBootApplication public class ThymeLeafApplication {public static void main(String[] args) {SpringApplication.run(ThymeLeafApplication.class,args);} }

application.yml
設(shè)置thymeleaf的緩存設(shè)置,設(shè)置為false。默認(rèn)加緩存的,用于測(cè)試。

spring:thymeleaf:cache: false

創(chuàng)建controller用于測(cè)試后臺(tái) 設(shè)置數(shù)據(jù)到model中。

@Controller @RequestMapping("/test") public class TestController { /** * 訪問(wèn)/test/hello 跳轉(zhuǎn)到demo1頁(yè)面 * @param model * @return */ @RequestMapping("/hello") public String hello(Model model){ model.addAttribute("hello","你好!趙麗穎!!!"); return "demo"; } }

創(chuàng)建html,在resources中創(chuàng)建templates目錄,在templates目錄創(chuàng)建 demo.html,代碼如下:

<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head><title>Thymeleaf的入門</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> </head> <body> <!--輸出hello數(shù)據(jù)--> <p th:text="${hello}"></p> </body> </html>
解釋:

<html xmlns:th="http://www.thymeleaf.org"> :這句聲明使用thymeleaf標(biāo)簽
<p th:text="${hello}"></p> :這句使用 th:text="${變量名}" 表示 使用thymeleaf獲取文本數(shù)據(jù),類似于EL表達(dá)式。

啟動(dòng)系統(tǒng),并在瀏覽器訪問(wèn)

http://localhost:8080/demo/hello

Thymeleaf基本語(yǔ)法

th:action 定義后臺(tái)控制器路徑,類似 <form> 標(biāo)簽的action屬性。
例如:

<form th:action="@{/test/hello}" > <input th:type="text" th:name="id"> <button>提交</button> </form>

th:each對(duì)象遍歷,功能類似jstl中的 <c:forEach> 標(biāo)簽。
Controller添加數(shù)據(jù)

/** * 訪問(wèn)/test/hello 跳轉(zhuǎn)到demo1頁(yè)面 * @param model * @return */ @RequestMapping("/hello") public String hello(Model model){model.addAttribute("hello","hello welcome"); //集合數(shù)據(jù) List<User> users = new ArrayList<User>(); users.add(new User(1,"張三","深圳")); users.add(new User(2,"李四","北京")); users.add(new User(3,"王五","武漢")); model.addAttribute("users",users); return "demo1"; }

頁(yè)面輸出

<table> <tr><td>下標(biāo)</td> <td>編號(hào)</td> <td>姓名</td> <td>住址</td> </tr> <tr th:each="user,userStat:${users}"> <td>下標(biāo):<span th:text="${userStat.index}"></span>, </td> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.address}"></td> </tr> </table>

Map輸出

//Map定義 Map<String,Object> dataMap = new HashMap<String,Object>(); dataMap.put("No","123"); dataMap.put("address","深圳"); model.addAttribute("dataMap",dataMap);

頁(yè)面輸出

<div th:each="map,mapStat:${dataMap}"> <div th:text="${map}"></div> key:<span th:text="${mapStat.current.key}"></span><br/> value:<span th:text="${mapStat.current.value}"></span><br/> </div>

數(shù)組輸出

//存儲(chǔ)一個(gè)數(shù)組 String[] names = {"張三","李四","王五"}; model.addAttribute("names",names);

頁(yè)面輸出

<div th:each="nm,nmStat:${names}"> <span th:text="${nmStat.count}"></span><span th:text="${nm}"></span> </div>

Date輸出,后臺(tái)添加日期

//日期 model.addAttribute("now",new Date());

頁(yè)面輸出

<div><span th:text="${#dates.format(now,'yyyy-MM-dd hh:ss:mm')}"></span> </div>

th:if條件

//if條件 model.addAttribute("age",22);

頁(yè)面輸出

<div><span th:if="${(age>=18)}">終于長(zhǎng)大了!</span> </div>

th:fragment 可以定義一個(gè)獨(dú)立的模塊,創(chuàng)建一個(gè)footer.html代碼如下:

<html xmlns:th="http://www.thymeleaf.org"> <head><meta http-equiv="Content-Type" content="text/html;charset=charset=utf-8"> <title>fragment</title> </head> <body> <div id="C" th:fragment="copy" > 關(guān)于我們<br/> </div> </body>

th:include 可以直接引入 th:fragment ,在demo1.html中引入如下代碼:

<div id="A" th:include="footer::copy"></div>

總結(jié)

以上是生活随笔為你收集整理的【模板引擎】Springboot整合ThymeleafThymeleaf基本语法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。