1、添加模板依赖
由于springboot已经整合了很多框架,所以我们添加类似于thymeleaf等框架,不需要添加版本号,直接写依赖即可
org.springframework.boot
spring-boot-starter-thymeleaf
2、分析SpringBoot依赖关系
在我们项目的pom.xml文件中,键盘Ctrl键按住,鼠标点击左键即可进去源码/文件上层
这里可以看到:spring-boot-starter-parent的pom.xml中的parent模块
我们继续Ctrl+鼠标左键,进去上层pom
这里就是SpringBoot的最顶层了:spring-boot-dependencies
如果springboot整合了一个框架,这里就可以搜索到,如果搜索不到,说明没有进行整合进来
我们这里搜索thymeleaf,Ctrl+F即可进行搜索内容
我们可以看到SpringBoot2.5.6版本整合的Thymeleaf版本是3.0.12
3、配置thymeleaf模板
#是否开启缓存默认为true
spring.thymeleaf.cache=false
#指定模板路径
spring.thymeleaf.prefix=classpath:/templates/
#设置thymeleaf严格校验
spring.thymeleaf.mode=html
#设置thymeleaf模板后缀
spring.thymeleaf.suffix=.html
#设置编码
spring.thymeleaf.encoding=utf-8
4、创建templates文件夹
在resources目录下创建templates文件夹,为thymeleaf的模板文件夹
5、写controller接口
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class CarController {
@RequestMapping("/getCar")
public String getCar(){
return "getCar";
}
}
6、写thymeleaf模板
在模板文件夹中创建模板文件html即可
Title
getCar
7、启动项目,并访问
http://localhost:8888/getCar