让我们考虑这个请求处理程序:
@GetMapping("/")
public Mono<String> index(Model model) {
model.addAttribute("list", Flux.just("item1", "item2"));
return Mono.just("list"); // <- Template name
}
如果templates
目录中有index.html
模板,则不会执行此处理程序。
如果我从处理程序返回 Mono.just("index")
:
@GetMapping("/")
public Mono<String> index(Model model) {
model.addAttribute("list", Flux.just("item1", "item2"));
return Mono.just("index"); // <- Template name
}
该处理程序仍未使用。
看来我必须从 templates
目录中删除 index.html
模板,才能将 GetMapping("/")
路由到予以处理。
所以我的问题是,是否可以从 GetMapping("/")
处理程序返回 Mono.just("index")
?
最佳答案
据此book ,您应该使用@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 - 是否可以从 GetMapping ("index")处理程序返回 Mono.just ("/")?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62550355/