SpringBoot整合Thymeleaf

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

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

二、编写Controller控制器

  1. @Controller
  2. public class MyController {
  3. @RequestMapping("/message")
  4. public String meeeage(Model model){
  5. model.addAttribute("data","Springboot集成Thymeleaf");
  6. return "message";
  7. }
  8. }

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

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

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

在springboot核心配置文件中写

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

开启资源更新

测试:

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

页面的内容也会更新

相关文章