博主
258
258
258
258
专辑

第二节 SpringBoot集成Thymeleaf模板

亮子 2022-02-14 11:48:02 6215 0 0 0

1、添加依赖

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

非springboot项目使用如下依赖:

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.11</version>
</dependency>

springboot1.4之后,可以使用thymeleaf3来提高效率,并且解决标签闭合问题,配置方式:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- set thymeleaf version -->
    <thymeleaf.version>3.0.0.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>
    <!--set java version-->
    <java.version>1.8</java.version>
  </properties>

也可以在创建项目的时候选择依赖:

IDEA+Thymeleaf

我的demo代码中使用SpringBoot的版本是2.3.7.RELEASE,它集成的Thymeleaf版本是3.0.11,如下图:

Thymeleaf版本

2、配置文件

# 在开发阶段,建议关闭thymeleaf的缓存
spring.thymeleaf.cache=false

实际项目中可能会有不太严格的HTML格式,此时设置mode=HTML5将会对非严格的报错,可以参考以下配置:

# 关闭标签检查
spring.thymeleaf.mode=LEGACYHTML5

下面是完整配置文件,仅供参考

# 应用名称
spring.application.name=server-demo-thymeleaf

# 应用服务 WEB 访问端口
server.port=8080

# THYMELEAF (ThymeleafAutoConfiguration)
# 开启模板缓存(默认值: true )
spring.thymeleaf.cache=true
# 检查模板是否存在,然后再呈现
spring.thymeleaf.check-template=true
# 检查模板位置是否正确(默认值 :true )
spring.thymeleaf.check-template-location=true
#Content-Type 的值(默认值: text/html )
spring.thymeleaf.content-type=text/html
# 开启 MVC Thymeleaf 视图解析(默认值: true )
spring.thymeleaf.enabled=true
# 模板编码
spring.thymeleaf.encoding=UTF-8
# 要被排除在解析之外的视图名称列表,⽤逗号分隔
spring.thymeleaf.excluded-view-names=
# 要运⽤于模板之上的模板模式。另⻅ StandardTemplate-ModeHandlers( 默认值: HTML5)
spring.thymeleaf.mode=HTML5
# 在构建 URL 时添加到视图名称前的前缀(默认值: classpath:/templates/ )
spring.thymeleaf.prefix=classpath:/templates/
# 在构建 URL 时添加到视图名称后的后缀(默认值: .html )
spring.thymeleaf.suffix=.html

4、去除标签校验

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

5、准备数据

package com.shenmazong.thymeleaf.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author 军哥
 * @version 1.0
 * @description: 测试Thymeleaf模板
 * @date 2022/2/16 9:59
 */

@Controller
@Slf4j
public class IndexController {
    @GetMapping(value = "/index")
    public String index(Model model) {
        model.addAttribute("title", "hello,world");
        return "index";
    }
}

6、创建模版

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>测试Thymeleaf模板</title>
</head>
<body>
    <div>
        <h1 th:text="${title}">Spring boot集成 Thymeleaf</h1>
    </div>
</body>
</html>

7、运行结果

Thymleaf运行结果