Spring boot集成Thymeleaf

簡介

Thymeleaf是用於Web和獨立開發環境的服務器端Java模板引擎。css

Thymeleaf的主要目標是爲開發工做流程帶來優雅的天然模板 - HTML能夠在瀏覽器中正確顯示,也能夠做爲靜態原型,從而在開發團隊中實現更強的協做。html

Maven使用Thymeleaf,首先須要引入依賴:web

<dependencies>  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-web</artifactId>  
    </dependency>  

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

開發環境須要關閉緩存application.ymlspring

spring:
    thymeleaf:
        cache: false  #默認true
        mode: HTML5   #默認HTML5

resources創建templatesbootstrap

在html中聲明th標籤瀏覽器

<html xmlns:th="http://www.thymeleaf.org">

而後就能夠在文件中使用th標籤了緩存

<link href="css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}" rel="stylesheet">

靜態資源的具體路徑以下服務器

配置完controller
app

簡單的例子就完成了。maven

須要注意的是,thymeleaf默認是嚴格校驗html的。

須要修改application.yml

spring:
    thymeleaf:
        mode: LEGACYHTML5

並增長maven依賴

<dependency>
    <groupId>nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.6.2</version>
</dependency>
相關文章
相關標籤/搜索