java - 是否可以从 GetMapping("/") 处理程序返回 Mono.just("index") ?
问题描述
让我们考虑这个请求处理程序:
@GetMapping("/")
public Mono<String> index(Model model) {
model.addAttribute("list", Flux.just("item1", "item2"));
return Mono.just("list"); // <- Template name
}
如果目录中有index.html
模板,templates
则不执行此处理程序。
如果我Mono.just("index")
从处理程序返回:
@GetMapping("/")
public Mono<String> index(Model model) {
model.addAttribute("list", Flux.just("item1", "item2"));
return Mono.just("index"); // <- Template name
}
该处理程序仍未使用。
看来我必须从目录中删除index.html
模板才能处理路由。templates
GetMapping("/")
所以我的问题是,是否可以Mono.just("index")
从GetMapping("/")
处理程序返回 a ?
解决方案
根据本书,你应该使用@Controller 而不是@RestController。
这是另一个示例:
@GetMapping("/welcome")
public Mono<String> hello(final Model model) {
model.addAttribute("name", "Foo");
model.addAttribute("city", "Bar");
String path = "hello";
return Mono.create(monoSink -> monoSink.success(path));
}
对应的thymeleaf html页面:
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8"/>
<title>Greet</title>
</head>
<body>
<p th:text="${name}"></p> lives in <p th:text="${city}"></p>
</body>
</html>
推荐阅读
- java - Java合并2个集合,同时根据特定字段比较和保留对象
- flutter - 什么是 Dart Flutter 中的零参数构造函数
- c# - 滚动查看所有记录 ElasticSearch C# Nest
- pdf - 使用 Apache Poi 库在 android 中将 docx 或 doc 转换为 PDF
- python - 如何使用python中的for循环从数据框中返回每列的最大值
- javascript - 使用 npm config 在 NodeJS 中配置文件
- hive - 在 Hive 中,有没有办法在 Y 列之前添加 X 列
- android - 为什么我录制的视频在使用改造上传到服务器后变成 0 字节?
- react-native - react-native 中的 ckeditor 实现
- java - 如何在数组列表中显示符号之前将数据存储到字符串?