1.pom文件
<!-- thymeleaf 模板引用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.application.yml
#配置thymeleaf渲染视图
thymeleaf:
prefix: classpath:/templates/
suffix: .html
cache: true
enabled: true
encoding: UTF-8
mode: HTML
3.resources文件夹下建文件夹
templates
4.controller设置访问路径
@Controller
public class IndexController {
@GetMapping("/login")
public String index(){
return "login";
}
}
5.login.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">//必须加
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<p style="color:red">hello world</p>
</body>
</html>
6.需要项目编译,在target目录下生成static和templates文件夹,否则访问页面不生效
问题例如
Error resolving template [login], template might not exist or might not be accessible by any of the
解决方案:
看了网上很多人解决方案都没解决我的 后来发现这个前缀少一个/
spring.thymeleaf.prefix=classpath:/templates/
如果不是这个可以再看看pom文件依赖引入没有,target目录有没有templates