我有这样的控制器:
@Controller
public class AuthController {
private final Logger logger= LoggerFactory.getLogger(AuthController.class);
private UserService userService;
public AuthController(UserService userService) {
this.userService = userService;
}
@GetMapping("login")
public String login(){
return "auth/login";
}
@GetMapping("/profile")
public String profile(){
return "auth/profile";
}
@GetMapping("/register")
public String register(Model model){
model.addAttribute("user",new User());
model.addAttribute("success",false);
return "auth/register";
}
@PostMapping("/register")
public String registerNewUser(@Valid User user, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes){
if(bindingResult.hasErrors()){
//show validation errors
logger.warn("Validation errors were found while registering a new user");
model.addAttribute("user",user);
model.addAttribute("validationErrrs",bindingResult.getAllErrors());
return "auth/register";
}else{
User newUser = userService.register(user);
redirectAttributes
.addAttribute("id",newUser.getId())
.addFlashAttribute("success",true);
logger.info("No validation errors were found while registerning a new user");
return "redirect:/register";
}
}
@GetMapping("/activate/{email}/{activationCode}")
public String activate(@PathVariable String email, @PathVariable String activationCode) {
Optional<User> user = userService.findByEmailAndActivationCode(email,activationCode);
if( user.isPresent() ) {
User newUser = user.get();
newUser.setEnabled(true);
newUser.setConfirmPassword(newUser.getPassword());
userService.save(newUser);
userService.sendWelcomeEmail(newUser);
return "auth/activated";
}
return "redirect:/";
}
}
我调用方法registerNewUser,并且我的记录器始终显示有关“注册新用户时未发现验证错误”的信息,因为执行了body。因此,我认为redirectAttributes.addFlashAttribute(“ success”,true);应该可以正常工作。 不幸的是,当我返回“ redirect:/ register”时,我得到了新的视图,但没有根据“ succes”属性来表示。 这是我的视图代码的一部分:
<div class="row" th:if="${success}">
<div class="col-md-3"></div>
<div class="col-md-6 alert alert-success" role="alert" >
You have successfully registered with us. Please check your email to
activate your account.
</div>
<div class="col-md-3"></div>
</div>
<!-- User Registration form -->
<form class="form-horizontal" role="form" method="POST"
th:action="@{/register}" th:object="${user}" th:if="!${success}">
<div class="row">
有人可以帮助我查明缺少警报的原因吗?
答案 0 :(得分:1)
您将在registerNewUser()
内重定向到GET /register
。这将调用以下方法:
@GetMapping("/register")
public String register(Model model){
model.addAttribute("user",new User());
model.addAttribute("success",false);
return "auth/register";
}
在这种方法中,您要添加模型属性success
,并将其设置为false
,从而有效地覆盖重定向属性。
由于使用RedirectAttributes
来填充重定向目标的Model
,因此您可以将model.addAttribute(..)
调用包装在if
语句中。例如:
@GetMapping("/register")
public String register(Model model){
model.addAttribute("user",new User());
if (!model.containsAttribute("success")) {
model.addAttribute("success",false);
}
return "auth/register";
}
或者,您可以保留model属性,因为解析器足够聪明,可以在丢失${success}
到false
时对其进行评估。例如:
@GetMapping("/register")
public String register(Model model){
model.addAttribute("user",new User());
return "auth/register";
}