Thymeleaf

Thymeleaf简介

Thymeleaf是一个风行的模板引擎,该模板引擎采纳Java语言开发,模板引擎是一个技术名词,是跨畛域跨平台的概念,在Java语言体系下有模板引擎,在C#、PHP语言体系下也有模板引擎。除了thymeleaf之外还有Velocity、FreeMarker等模板引擎,性能相似。
Thymeleaf的次要指标在于提供一种可被浏览器正确显示的、格局良好的模板创立形式,因而也能够用作动态建模。你能够应用它创立通过验证的XML与HTML模板。应用thymeleaf创立的html模板能够在浏览器外面间接关上(展现静态数据),这有利于前后端拆散。须要留神的是thymeleaf不是spring旗下的。这里咱们应用thymeleaf 3版本。

第一个thymeleaf程序


增加thymeleaf依赖
<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>


批改spring boot配置文件

在开发阶段,倡议敞开thymeleaf的缓存(在application.properties文件中设置)

spring.thymeleaf.cache=false


thymeleaf会对html中的标签进行严格校验,如果html标签短少完结标签的话,thymeleaf会报错,咱们能够通过上面形式去除thymeleaf的校验,增加依赖:

 <dependency>      <groupId>net.sourceforge.nekohtml</groupId>      <artifactId>nekohtml</artifactId>      <version>1.9.22</version>  </dependency> 


在spring boot配置文件中增加上面内容:

spring.thymeleaf.mode=LEGANCYHTML5