1. 程式人生 > 其它 >1.4 Springboot整合Thymeleaf

1.4 Springboot整合Thymeleaf

技術標籤:SpringBootxmlspring bootjavaspringhtml

SpringBoot 整合Thymeleaf

說明:
Thymeleaf 是一個跟 Velocity、FreeMarker 類似的模板引擎,它可以完全替代 JSP 。
相較與其他的模板引擎,它有如下三個極吸引人的特點:

1. Thymeleaf 在有網路和無網路的環境下皆可執行,即它可以讓美工在瀏覽器檢視頁面的靜態效果,也可以讓程式設計師在伺服器檢視帶資料的動態頁面效果。這是由於它支援 html 原型,然後在 html 標籤裡增加額外的屬性來達到模板 + 資料的展示方式。瀏覽器解釋 html 時會忽略未定義的標籤屬性,所以 thymeleaf 的模板可以靜態地執行;當有資料返回到頁面時,Thymeleaf 標籤會動態地替換掉靜態內容,使頁面動態顯示。
2. Thymeleaf 開箱即用的特性。它提供標準和 Spring 標準兩種方言,可以直接套用模板實現 JSTL、 OGNL 表示式效果,避免每天套模板、改 JSTL、改標籤的困擾。同時開發人員也可以擴充套件和建立自定義的方言。
3. Thymeleaf 提供 Spring 標準方言和一個與 SpringMVC 完美整合的可選模組,可以快速的實現表單繫結、屬性編輯器、國際化等功能。

此篇是基於以下文章繼續創作。
1.1 搭建Springboot腳手架
1.2 搭建SpringBoot腳手架註解及標籤說明
1.3 SpringBoot 整合熱部署

目錄

模組配置項備註
新增依賴pom.xml額外新增nekohtml依賴,解決thymealeaf標籤閉合問題
配置檔案application.properties配置檔案不允許有多餘空格
測試控制類TestController.java可以跳轉到templates資料夾下的html頁面並且傳遞引數
HTML頁面login.html接收控制類傳遞的資料加以處理

專案結構

下圖為Eclipse中的專案結構截圖
專案結構截圖
說明:
在此之前需要構建resources資料夾
新建static和templates資料夾
靜態資源存放在static資料夾下
html檔案要存放在templates資料夾下

注:
新建templates和static資料夾時,要直接在resources資料夾上new Folder,否則之後controller層跳轉頁面會出錯

1. pom.xml

注: 在dependencies標籤內加入如下程式碼

<!--thymeleaf-->
	<dependency>
   	 <groupId>org.springframework.boot</groupId>
   	 <artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>

<!--nekohtml 解決thymealeaf標籤閉合問題-->
	<dependency>
    	<groupId>net.sourceforge.nekohtml</groupId>
    	<artifactId>nekohtml</artifactId>
    	<version>1.9.14</version>
	</dependency>

2. application.properties

注: 配置檔案程式碼不允許有空格,否則會報錯

# ====================================================================
# ===================Thymeleaf 相關設定============================
# ====================================================================

# 是否開啟模板快取,預設true,開發時關閉快取,不然沒法看到實時頁面
spring.thymeleaf.cache=false
    
# 指定模板的編碼,預設為: UTF-8
spring.thymeleaf.encoding=UTF-8

#返回模板型別
spring.thymeleaf.content-type=text/html

# 指定模板的字首(檔案位置),預設為:classpath:/templates/
spring.thymeleaf.prefix=classpath:/templates/
  
# 指定模板的字尾,預設為:.html
spring.thymeleaf.suffix=.html

# 指定模板的模式, 預設為:HTML5 (如果使用了nekohtml依賴 設定為LEGACYHTML5,去除thymeleaf的html嚴格校驗)
spring.thymeleaf.mode=LEGACYHTML5

#預設值為/**,與下面locations成對使用
#spring.mvc.static-path-pattern=/**
#自定義Spring boot載入前端靜態資源路徑
spring.resources.static-locations=classpath:/templates/,classpath:/static/

3. MainController.java

3.1 MainController1.java

注: 新增控制類測試跳轉頁面

package org.it.Demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MainController {
	
	@RequestMapping("/index")
	public String index() {
		return "index";//返回到index.html頁面
	}
	
}

3.2 MainController2.java

注: 新增控制類測試返回資料

package org.it.Demo.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
	
	@RequestMapping("/")
	public String index(ModelMap map) {
		
		//單個數據
        map.put("hello", "這是顯示歡迎資訊");
		return "3";
	}	
    
}

4. 3.html

注:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!--使用Thymeleaf標籤時,需在頭部引入<html lang="en" xmlns:th="http://www.thymeleaf.org">,
否則會報錯-->
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>成功!</h1>
    <!--th:text 將div裡面的文字內容設定為 -->
    <div th:text="${hello}">aa</div>
</body>
</html>

詳細使用Thymeleaf參考資料:

1. Springboot之Thymeleaf 入門(環境搭建)
2. CSDN-史上最詳 Thymeleaf 使用教程
3. Thymeleaf 教程:使用Thymeleaf[官方]