spring - 无法在 Spring Boot 中解析名称为“index”的视图
问题描述
春季启动:2.3.3.RELEASE
爪哇: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);
}
}
解决方案
负责处理静态文件位置的默认属性是spring.resources.static-locations
.
默认值为/META-INF/resources/, /resources/, /static/, /public/
。您可以覆盖默认值或将您的值放在index.html
这些位置之一。
推荐阅读
- regex - 正则表达式按空格分割,单引号内的字符串除外
- asp.net-mvc - 如何在 ASP.net Core / 5 中建模绑定动态单选按钮
- javascript - 流程:无法创建采用泛型或对象的反应组件
- gruntjs - 所有 grunt 任务的默认任务选项
- python - 多个条件然后创建一列
- sublimetext3 - 如何使用 Emmet 在 Sublime 中添加非 html 语法片段?
- api - 如何为特定的 API 控制器指定错误处理程序?
- python - 访问主程序中正在修改的线程中的变量
- google-chrome - 清单 v3 中的 chrome.runtime 缺少属性
- azure - Terraform azurerm role definition with email address