1.導入依賴:
<!--訪問模板文件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.application.properties文件配置:
#配置模板引擎
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.cache=false
3.controller類和html模板文件:
controller:
@Controller
@RequestMapping("/index")
public class HomeController {
@RequestMapping("/hello")
public String index() {
return "home";
}
}
resource/template 文件夾下的home.html文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
hello springboot
</body>
</html>