整合Thymeleaf

HeJin小于 1 分钟Spring全家桶SpringBoot精讲细讲

引入依赖

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

引入thymeleaf依赖之后,视图解析器就会走thymeleaf的。

网页中使用thymeleaf

参考thymeleaf官方文档,即可使用相应的语法。

https://www.thymeleaf.org/open in new window

image-20210512150204030
image-20210512150204030

controller

@RestController
public class UserController {
    @RequestMapping("/")
    public String index(){
        return "hello thymeleaf";
    }

}

前端页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>

    <span th:text="${msg}"></span>

</body>
</html>

访问请求,即可得到结果:

image-20210512150615578
image-20210512150615578