- 添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- application.properties相关配置
#整合thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.prefix=classpath:/templates/tl/
spring.thymeleaf.suffix=.html
- Controller类
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class ThymeleafController {@RequestMapping("/index")
public String index(Model model) {
model.addAttribute("msg", "hello world");
return "index";
}
}
- 测试页面:/templates/tl/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<p th:text="${msg}"></p>
</body>
</html>