1、添加maven依赖
<!-- 用于解析html -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、配置文件:application.properties
##thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.mvc.view.suffix=.html
spring.mvc.static-path-pattern=/static/**
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML
spring.devtools.restart.enabled=true
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.enabled=true
3、目录结构
在resources下 新建 static & templates 两个目录,
static保存静态资源文件,templates保存html文件
4、新建Controller 访问路径
@Controller
public class IndexController {
@RequestMapping({"/","/home"})
public String index(HashMap map){
return "home";
}
}
访问localhost:8001/home 会跳转到home.html页面,
@ResponseBody
方法注解有这个,说明以json传值,不会返回页面,只返回json字符串。一般ajax会用到。
类注解 @Controller + 方法注解 @ResponseBody
相当于 类注解@RestController
5、新建页面 home.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">//代表 使用thymeleaf框架
<head>
<meta charset="utf-8">
</head>
<body class="sticky-header">
Hello,Springboot!
</body>
</html>
静态资源引入路径:/static/js/...