Fork me on GitHub

springboot+Thymeleaf模板

首先在pom.xml中加入Thymeleaf依赖

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>

application.yml 设置 thyemleaf

1
2
3
4
5
6
7
8
spring:
thymeleaf:
cache: false
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
content-type: text/html
mode: HTML5

新建H5页面

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>

但是这样的话springboot访问时候会出现页面错误,因为springboot默认使用Thymeleaf2.0,2.0版本对语法格式要求比较严格,比如代码的结尾一定要/>
两种解决错误方式

  • –一是修改
1
<meta charset="UTF-8"/>
  • –二是修改Thymeleaf版本,3.0对格式要求放宽了
    在pom.xml中加入
1
2
3
4
<properties>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>

在H5中加入Thymeleaf

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

关于Thymeleaf标签

-------------本文结束感谢您的阅读-------------