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">

静态资源的具体路径以下服务器

配置完controllerapp

简单的例子就完成了。maven

须要注意的是,thymeleaf默认是严格校验html的。

须要修改application.yml

spring:
    thymeleaf:
        mode: LEGACYHTML5

并增长maven依赖

<dependency>
    <groupId>nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.6.2</version>
</dependency>
相关文章
相关标签/搜索