resources目录下

建 application.properties文件


内容如下

  1. spring.thymeleaf.prefix=classpath:/templates/
  2. spring.thymeleaf.suffix=.html
  3. spring.thymeleaf.cache=false
  4. spring.thymeleaf.content-type=text/html
  5. spring.thymeleaf.enabled=true
  6. spring.thymeleaf.encoding=UTF-8
  7. spring.thymeleaf.mode=HTML5

建templates文件夹


再在templates文件夹下建一个hello.html文件,内容如下

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <p th:text="${hello}"></p>
  9. </body>
  10. </html>

controller


  1. @Controller
  2. public class TestController {
  3. @GetMapping("/hello")
  4. public String thymeleafTest(Model model){
  5. model.addAttribute("hello","hello");
  6. return "hello";
  7. }
  8. }

启动


访问localhost:8080/hello