spring boot访问页面需要thymeleaf模板依赖
在pom文件中引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
1.直接访问页面
你只需要创建一个新的html文件 放到resource目录下的static目录下即可
然后直接localhost:8888/*.html(*代指的是你的页面名称)
2.通过控制层访问页面
此时你的页面应该放的位置templates目录下(同理:如果你没有该目录,你可以自行创建即可)
在controller文件中
@Controller
@RequestMapping("demo")
public class Demo {
@RequestMapping("/demo")
public String demo(){
return "demo3";
} }
3.yml配置(可选)
spring:
# thyleaf模板
thymeleaf:
prefix: classpath:/templates/
suffix: html
mode: HTML5
encoding: UTF-8
content-type: text/html
cache: false
访问:localhost:8080/demo/demo即可
本文介绍如何在SpringBoot项目中使用Thymeleaf模板引擎,包括配置依赖、页面放置位置及控制器映射等关键步骤。
2161





