SpringBoot整合Thymeleaf

x33g5p2x  于2021-11-28 转载在 Spring  
字(0.9k)|赞(0)|评价(0)|浏览(351)

一、创建工程并选择起步依赖

二、编写Controller控制器

@Controller
public class MyController {
    @RequestMapping("/message")
    public String meeeage(Model model){
        model.addAttribute("data","Springboot集成Thymeleaf");
        return "message";
    }
}

三、在templates文件夹下创建html文件

<!DOCTYPE html>
<!-- xmlns:th="http://www.thymeleaf.org" 这个是Thymeleaf的命名空间(必须要加这个) -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Message</title>
</head>
<body>
<!-- th:text="${data}"这里的数据内容会覆盖掉标签内的内容 -->
<h1 th:text="${data}">Hello Thymeleaf</h1>
</body>
</html>

四、关闭Thymeleaf缓存并开启资源更新

在springboot核心配置文件中写

#配置Thymeleaf模板引擎的前后缀(可不写)
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
#关闭Thymeleaf模板引擎缓存
spring.thymeleaf.cache=false

开启资源更新

测试:

在页面上输出的是data而不是标签的内容
在message.html文件里修改代码后刷新浏览器(不重启服务器)

页面的内容也会更新

相关文章