javascript
【模板引擎】Springboot整合ThymeleafThymeleaf基本语法
Thymeleaf介紹
thymeleaf是一個XML/XHTML/HTML5模板引擎,可用于Web與非Web環境中的應用開發。它是一個開源的Java庫,基于Apache License 2.0許可,由Daniel Fernández創建,該作者還是Java加密庫Jasypt的作者。
Thymeleaf提供了一個用于整合Spring MVC的可選模塊,在應用開發中,你可以使用Thymeleaf來完全代替JSP或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目標在于提供一種可被瀏覽器正確顯示的、格式良好的模板創建方式,因此也可以用作靜態建模。你可以使用它創建經過驗證的XML與HTML模板。相對于編寫邏輯或代碼,開發者只需將標簽屬性添加到模板中即可。接下來,這些標簽屬性就會在DOM(文檔對象模型)上執行預先制定好的邏輯。
它的特點便是:開箱即用,Thymeleaf允許您處理六種模板,每種模板稱為模板模式:
* XML
* 有效的XML
* XHTML
* 有效的XHTML
* HTML5
* 舊版HTML5
所有這些模式都指的是格式良好的XML文件,但Legacy HTML5模式除外,它允許您處理HTML5文件,其中包含獨立(非關閉)標記,沒有值的標記屬性或不在引號之間寫入的標記屬性。為了在這種特定模式下處理文件,Thymeleaf將首先執行轉換,將您的文件轉換為格式良好的XML文件,這些文件仍然是完全有效的HTML5(實際上是創建HTML5代碼的推薦方法)。
另請注意,驗證僅適用于XML和XHTML模板。
然而,這些并不是Thymeleaf可以處理的唯一模板類型,并且用戶始終能夠通過指定在此模式下解析模板的方法和編寫結果的方式來定義他/她自己的模式。這樣,任何可以建模為DOM樹(無論是否為XML)的東西都可以被Thymeleaf有效地作為模板處理。
Springboot整合thymeleaf
使用springboot 來集成使用Thymeleaf可以大大減少單純使用thymleaf的代碼量
pom.xml依賴
啟動類ThymeleafApplication
@SpringBootApplication public class ThymeLeafApplication {public static void main(String[] args) {SpringApplication.run(ThymeLeafApplication.class,args);} }application.yml
設置thymeleaf的緩存設置,設置為false。默認加緩存的,用于測試。
創建controller用于測試后臺 設置數據到model中。
@Controller @RequestMapping("/test") public class TestController { /** * 訪問/test/hello 跳轉到demo1頁面 * @param model * @return */ @RequestMapping("/hello") public String hello(Model model){ model.addAttribute("hello","你好!趙麗穎!!!"); return "demo"; } }創建html,在resources中創建templates目錄,在templates目錄創建 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數據--> <p th:text="${hello}"></p> </body> </html>解釋:
<html xmlns:th="http://www.thymeleaf.org"> :這句聲明使用thymeleaf標簽
<p th:text="${hello}"></p> :這句使用 th:text="${變量名}" 表示 使用thymeleaf獲取文本數據,類似于EL表達式。
啟動系統,并在瀏覽器訪問
http://localhost:8080/demo/helloThymeleaf基本語法
th:action 定義后臺控制器路徑,類似 <form> 標簽的action屬性。
例如:
th:each對象遍歷,功能類似jstl中的 <c:forEach> 標簽。
Controller添加數據
頁面輸出
<table> <tr><td>下標</td> <td>編號</td> <td>姓名</td> <td>住址</td> </tr> <tr th:each="user,userStat:${users}"> <td>下標:<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);頁面輸出
<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>數組輸出
//存儲一個數組 String[] names = {"張三","李四","王五"}; model.addAttribute("names",names);頁面輸出
<div th:each="nm,nmStat:${names}"> <span th:text="${nmStat.count}"></span><span th:text="${nm}"></span> </div>Date輸出,后臺添加日期
//日期 model.addAttribute("now",new Date());頁面輸出
<div><span th:text="${#dates.format(now,'yyyy-MM-dd hh:ss:mm')}"></span> </div>th:if條件
//if條件 model.addAttribute("age",22);頁面輸出
<div><span th:if="${(age>=18)}">終于長大了!</span> </div>th:fragment 可以定義一個獨立的模塊,創建一個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" > 關于我們<br/> </div> </body>th:include 可以直接引入 th:fragment ,在demo1.html中引入如下代碼:
<div id="A" th:include="footer::copy"></div>總結
以上是生活随笔為你收集整理的【模板引擎】Springboot整合ThymeleafThymeleaf基本语法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【工具类】分布式文件存储-FastDFS
- 下一篇: 【分布式】分布式事务解决方案概述