1. Add the JAR package
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
Copy the code
2. Configuration file Settings
spring.thymeleaf.cache=falsespring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.htmlspring.thymeleaf.e ncoding=UTF-8spring.thymeleaf.content-type=text/htmlspring.thymeleaf.mode=LEGACYHTML5Copy the code
3. Java background code
@Controllerpublic class TestController { @Autowired TestService testService; @RequestMapping("/") public String testThymeleaf(ModelMap modelMap){ modelMap.addAttribute("msg", "helloworld"); return "index"; }}Copy the code
4. Page design
You must put it in the specified folder, which is resource/templates, as follows
<! DOCTYPE html><html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"><head><meta charset="UTF-8"><title>index</title></head><body><h1 th:text="${msg}"></h1></body></html>Copy the code