SpringBoot / MVC&使用URL参数在POST上进行Thymleaf表单验证

时间:2015-04-27 21:27:52

标签: validation spring-mvc spring-boot thymeleaf

我有表格和验证工作。添加url参数时会出现此问题。 url参数是一个标记,是必需的。所以这就是我的控制器的样子:

@RequestMapping(value = "/resetpassword", method = RequestMethod.GET)
public String showResetForm(ResetPassword resetPassword, Model model,
        @RequestParam(value = "token", required = true) String token,
        @RequestParam(value = "msg", required = false) String msg){     

        model.addAttribute("token", token);

    return "resetpassword";
}

@RequestMapping(value = "/resetpassword", method = RequestMethod.POST)
public String setPwd(@ModelAttribute("resetPassword") @Valid ResetPassword resetPassword,// RedirectAttributes reDirectAttr,
                     BindingResult bindingResult, Model model,
                     @RequestParam(value = "token", required = true) String token,
                     @RequestParam(value = "msg", required = false) String msg){

    if (bindingResult.hasErrors()) {
            //reDirectAttr.addFlashAttribute("org.springframework.validation.BindingResult.resetPassword",bindingResult);
            //reDirectAttr.addFlashAttribute("resetPassword",resetPassword);            
        return "resetpassword?token="+token;
    } 
    else {          
        if (token == null) {
            // TODO: no token, what to do here??
            return "redirect:/resetpassword?token=\"\"&msg=notoken";
        }
        ResetPasswordResponseDto response = super.resetUserPassword(
                resetPassword.getUname(), resetPassword.getPassword(),
                token);
        if (response.getPasswordResetResult() == PasswordResetResult.SUCCESSFUL) {
            // TODO: it worked, what now?
            return "redirect:/login";
        } else if (response.getPasswordResetResult() == PasswordResetResult.INVALID_TOKEN) {
            // TODO: bad token
            return "redirect:/resetpassword?token="+token+"&msg=badtoken";
        } else if (response.getPasswordResetResult() == PasswordResetResult.OUT_OF_POLICY_PW) {
            // TODO: out of policy pw
            return "redirect:/resetpassword?token="+token+"&msg=outofpolicy";
        } else if (response.getPasswordResetResult() == PasswordResetResult.LDAP_FAILURE) {
            // TODO: other failure
            return "redirect:/resetpassword?token="+token+"&msg=error";
        }
    }
    return "redirect:/resetpassword?token="+token+"&msg=error";
    //return new RedirectView("resetpassword?token=\"\"&msg=notoken");
}

所以我尝试了很多东西但似乎没什么用。当请求视图/ resetpassword时,我希望发生这种情况?token = 1232453显示视图。然后,如果表单有错误,url参数将保留在url中,并且表单会显示错误。现在我收到一个错误,说模板无法解析。好吧,所以我尝试重新定向

return "redirect:/resetpassword?token="+token;

这似乎有效,但URL参数丢失,视图丢失bindingResult错误。在代码中,我发布了我也尝试了FlashAttributes,但我只是得到一个错误"对象=' resetPassword'验证失败。错误计数:4"这是正确的,但我需要它来显示我用Thymeleaf编码的形式和错误。任何帮助或建议都会很棒!

我看过的资源: Spring - Redirect after POST (even with validation errors) &安培; SpringMVC controller: how to stay on page if form validation error occurs

1 个答案:

答案 0 :(得分:0)

您是否尝试过返回ModelAndView而不仅仅是重定向字符串?模型上的属性将作为URL查询参数提供。

ModelAndView redirect = new ModelAndView("redirect:/resetpassword");
redirect.addObject("token", token);
redirect.addObject("msg", "error");
return redirect;