Spring Boot无法返回html文件

问题描述 投票:0回答:1

虽然我将@Controller更改为@RestContoller,但它打印的不是index.html而是“index”

它应该将 html 文件放入模板文件夹中,但它不起作用

我还尝试添加一些属性

spring.thymeleaf.prefix=/templates
spring.thymeleaf.suffix=.html

在 application.properties 中,它仍然不起作用

提前感谢您的帮助

html spring-boot templates controller
1个回答
0
投票

确保你的pom包含thymeleaf依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

将模板放在

src/main/resources/templates
文件夹下。默认扩展名是
.html

确保您的控制器有

@RequestMapping
注释

@Controller
@RequestMapping("/")
class MainController {

  @GetMapping
  public String viewHomePage() {
    return "index";
  }
}

application.properties

删除所有配置
© www.soinside.com 2019 - 2024. All rights reserved.