SpringBoot 整合 Thymeleaf

添加依赖

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

配置文件

文件前缀后缀默认,前缀是 /templates/,后缀是.html

spring:
  thymeleaf:
    #关闭缓存
    cache: false

测试

controller

    @GetMapping("index")
    public String index(Model model){
        model.addAttribute("username","WACANDA FROEVER");
        return "index";
    }

 html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf</title>
</head>
<body>
<h1 th:text="${username}"></h1>
</body>
</html>

结果

PS:在thymeleaf使用中可能会碰到th:href,th:onclick的相关问题,可以参考

https://blog.csdn.net/zh137289/article/details/108368941

猜你喜欢

转载自blog.csdn.net/zh137289/article/details/108366494
今日推荐