Thymeleaf是一種用於Web和獨立環境的現代服務器端的Java模板引擎。html
Thymeleaf的主要目標是將優雅的天然模板帶到開發工做流程中,並將HTML在瀏覽器中正確顯示,而且能夠做爲靜態原型,讓開發團隊能更容易地協做。Thymeleaf可以處理HTML,XML,JavaScript,CSS甚至純文本。前端
Thymeleaf使用Spring框架的模塊,與許多常見的工具集成在一塊兒,而且能夠插入本身的功能,是現代HTML5 JVM Web開發的理想選擇,儘管Thymeleaf還有更多其它的功能。java
Thymeleaf創建在天然模板的概念之上,以不影響模板做爲設計原型的方式將其邏輯注入到模板文件中。 這改善了設計溝通,彌合了前端設計和開發人員之間的理解誤差。
web
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
使用Thymeleaf模板在Spring Boot中建立Web應用程序。必須按照如下步驟使用Thymeleaf在Spring Boot中建立Web應用程序。spring
使用如下代碼建立@Controller註解類文件以將Request URI重定向到HTML文件 瀏覽器
package com.sicq.publicity.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping(value = "home") public class HomeController { @RequestMapping(value = "/index") public String index() { return "index"; } }
在上面的示例中,請求URI是/index,被重定向到index.html文件。 請注意,index.html 文件應放在templates目錄下,全部JS和CSS文件應放在static目錄下。 服務器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h4>Thymeleaf Spring Boot web應用程序示例</h4> </body> </html>
啓動應用程序,打開瀏覽器訪問URL => http://localhost:8080/home/index.app