Post-Redirect-Get with Spring WebFlux 和 Thymeleaf
Post-Redirect-Get with Spring WebFlux and Thymeleaf
有人可以向我解释如何在 Spring WebFlux 和 Thymeleaf 中实现 post-redirect-get 模式吗? 什么订阅数据库保存方法?
@GetMapping("/register")
public String showRegisterForm(Model model) {
model.addAttribute("user", new User());
return "register";
}
@PostMapping
public String processRegisterForm(@Valid User user, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "register";
} else {
userRepository.save(user); //what subscribes on this?
//how to redirect on e.g. "/login"?
}
}
你可以让你的控制器方法 return 像这样的反应类型:
@PostMapping
public Mono<String> processRegisterForm(@Valid User user, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return Mono.just("register");
} else {
return userRepository.save(user).thenReturn("redirect:/login");
}
}
仅作记录,我推荐 Brian 编写的解决方案,因为它能更好地表达意图。但是,如果你想打动你的朋友。这里是一些没有if语句的。
@PostMapping
public Mono<String> processRegisterForm(@Valid User user, BindingResult bindingResult) {
return Mono
.just(bindingResult.hasErrors())
.filter(t -> t)
.flatMap( t-> Mono.just("register"))
.switchIfEmpty(userRepository.save(user).thenReturn("redirect:/login"));
}
有人可以向我解释如何在 Spring WebFlux 和 Thymeleaf 中实现 post-redirect-get 模式吗? 什么订阅数据库保存方法?
@GetMapping("/register")
public String showRegisterForm(Model model) {
model.addAttribute("user", new User());
return "register";
}
@PostMapping
public String processRegisterForm(@Valid User user, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "register";
} else {
userRepository.save(user); //what subscribes on this?
//how to redirect on e.g. "/login"?
}
}
你可以让你的控制器方法 return 像这样的反应类型:
@PostMapping
public Mono<String> processRegisterForm(@Valid User user, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return Mono.just("register");
} else {
return userRepository.save(user).thenReturn("redirect:/login");
}
}
仅作记录,我推荐 Brian 编写的解决方案,因为它能更好地表达意图。但是,如果你想打动你的朋友。这里是一些没有if语句的。
@PostMapping
public Mono<String> processRegisterForm(@Valid User user, BindingResult bindingResult) {
return Mono
.just(bindingResult.hasErrors())
.filter(t -> t)
.flatMap( t-> Mono.just("register"))
.switchIfEmpty(userRepository.save(user).thenReturn("redirect:/login"));
}