1.在pom文件中添加对thymeleaf的引用
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>2.编写controller层 注意controller层要在application.java[就是带有main方法的那个文件]包同级或者下级,否则可能会出席扫描不到等异常
import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class IndexController { @RequestMapping("/") public String index(ModelMap map) { // 加入一个属性,用来在模板中读取 map.addAttribute("name", "Hello Spring Boot!!"); // return模板文件的名称,对应路径为src/main/resources/templates/index.html return "index"; } }3.在src/main/resources/templates/目录下创建index.html
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title>test</title> </head> <body> <h1 th:text="${name}">Hello World</h1> test </body> </html>大功告成!!