javascript
Spring Boot整合Thymeleaf模板引擎
轉載自?Spring Boot整合Thymeleaf模板引擎
什么是Thymeleaf
Thymeleaf是一款用于渲染XML、XHTML、HTML5內容的模板引擎。類似Velocity,FreeMaker模板引擎,它也可以輕易的與Spring MVC等Web框架進行集成作為Web應用的模板引擎。
Thymeleaf也是Spring Boot首要支持的模板引擎,并且在最新的Spring Boot版本中已經不再支持Velocity了。
官網:http://www.thymeleaf.org/
引入依賴
需要引入Spring Boot的Thymeleaf啟動器依賴。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>引入該依賴后會自動引入web依賴,不需要再單獨引入web依賴。
自動配置說明
下面是Thymeleaf的自動配置相關類。
Thymeleaf的自動配置類:
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration
Thymeleaf的自動配置參數類:
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
查看參數源碼:
private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8"); private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html"); public static final String DEFAULT_PREFIX = "classpath:/templates/"; public static final String DEFAULT_SUFFIX = ".html";默認的編碼是:UTF-8
默認的類型是:text/html
默認的模板文件目錄是:classpath:/templates/
默認的模板文件后綴是:.html
這些參數都可以通過在application配置文件中指定 spring.thymeleaf.xx進行更改,更多可參考該參數類。
實戰
知道了自動配置的原理,所以我們可以知道怎么做了。
一、在resources目錄下創建templates目錄。
二、在templates目錄下創建.html模板文件。
三、使用模板:
1、模板文件頭部使用 <htmlxmlns:th="http://www.thymeleaf.org">定義。
2、html標簽上使用 th:開頭標識作為前綴。
3、通過 @{}引入web靜態文件。
<link rel="stylesheet" th:href="@{/css/jquery.min.css}"/>4、訪問數據
訪問springmvc中的model數據: ${user.name},訪問更多不同對象的數據請點擊參考官方定義。
總結
以上是生活随笔為你收集整理的Spring Boot整合Thymeleaf模板引擎的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2022年6个最佳演员网站建设者+优秀模
- 下一篇: Spring面试题(第一期)