关于java:Thymeleaf-配置等注意事项

4次阅读

共计 849 个字符,预计需要花费 3 分钟才能阅读完成。

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


正文完
 0