- 在pom文件中,将这个版本改为2.6.8
org.springframework.boot spring-boot-starter-parent 2.6.8
- 添加thymeleaf依赖
org.springframework.boot spring-boot-starter-thymeleaf
- 控制器注解用**@Controller**,因为controller中定义对应的访问路由及返回的页面,而@RestController没有
@Controller //thymeleaf 依赖和@Controller可跳转return页面
@RequestMapping("user")
public class UserController {
@GetMapping("/test")
public String test(){
return "test";
}
}
- SpringMVC配置【此配置为默认配置,无需添加,更改时需自定义】
spring: mvc: view: prefix:/suffix: .html
- 当然,html文件默认放在classpath:/templates下【无需添加】,如果要自定义路径,需要在yml配置文件中自定义配置
自定义配置如下:
spring: thymeleaf: suffix: .html prefix: classpath:/xx/xx/
比如配置为:prefix: classpath:/templates/,现要返回templates/test/test.html,
controller就要返回 “test/test”
至此,完结。有用点个赞呗



