【问题标题】:Could not resolve view with name 'index' in Spring Boot无法在 Spring Boot 中解析名称为“index”的视图
【发布时间】:2020-12-11 02:53:18
【问题描述】:

春季启动:2.3.3.RELEASE

Java:11

我使用 webflux + RouterFunction + Thymeleaf 并遇到错误“无法解析名称为 'index' 的视图”。

index.html 在“资源/模板”下。 我放了一些看起来很重要的源代码。

如果我们使用“RouterFunction”,我们就不能使用 Thymeleaf 了吗?

如果您需要更多详细信息,请随时发表评论。

######## handler ###########
@Component
public class ItemHandler {

public RouterFunction<ServerResponse> routes = route()
        .path("/item", builder -> builder.GET("/", this::index))
        .build();

public Mono<ServerResponse> index(ServerRequest request) {
    Map<String, Object> attributes = new HashMap<>();
    attributes.put("items", "Hello");

    return ServerResponse.ok().contentType(MediaType.TEXT_HTML)
            .render("index", attributes);
}
}


######## index.html ###########
<!DOCTYPE html>
<html lang="ja"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<h1>FluxTest</h1>
</body>
</html>

######## entry point ###########
@SpringBootApplication
public class DemoWebfluxApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoWebfluxApplication.class, args);
    }
}

【问题讨论】:

  • 我看到了 inde.html,试试 inde
  • 很抱歉。错字。名称是 index.html。

标签: spring spring-boot thymeleaf spring-webflux


【解决方案1】:

负责处理静态文件位置的默认属性是spring.resources.static-locations

默认值为/META-INF/resources/, /resources/, /static/, /public/。您可以覆盖默认值或将 index.html 放在这些位置之一。

【讨论】:

  • spring.resources.static-locations 已弃用,而应使用 spring.web.resources.static-locations
猜你喜欢
  • 2018-12-20
  • 2016-12-27
  • 2018-12-13
  • 1970-01-01
  • 2015-08-06
  • 1970-01-01
  • 2017-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多