1、在项目中添加依赖项,以下2个缺一不可。
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring5 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2、添加控制器
package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @Controller public class HelloController { @RequestMapping("/test") public String test(Model model){ model.addAttribute("msg","hello,springboot"); return "test"; } }
3、 在templates中添加视图模板,视图模板的名称一定要是html结尾的扩展名。
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> index test!! <p th:text="${msg}"></p> </body> </html>
4、启动运行。
标签:web,使用,springframework,案例,Thymeleaf,org,test,import,annotation From: https://www.cnblogs.com/huaan011/p/17776256.html