Spring - POST后重定向(即使有验证错误) [英] Spring - Redirect after POST (even with validation errors)

查看:206
本文介绍了Spring - POST后重定向(即使有验证错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何保留BindingResult,以便通过Spring < form:errors> 标签在后续的GET中使用它。我想这样做的原因是因为Google App Engine的SSL限制。我有一个通过HTTP显示的表单,并且这个表单是一个HTTPS URL。如果我只是转发而不是重定向,那么用户会看到 https://whatever.appspot.com/my/表单网址。我试图避免这一点。任何想法如何处理这个?



以下是我想要做的,但我只看到验证错误,当我使用 returncreate

  @RequestMapping(value =/ submit,method = RequestMethod.POST)
public final String submit(
@ModelAttribute(register)@Valid final Register register,
final BindingResult binding){

if(binding.hasErrors()) {
returnredirect:/ register / create;
}

返回redirect:/ register / success;


$ <$ pre

解决方案

RedirectAttributes。在执行重定向之前添加您想要的可用属性。添加BindingResult和你用来验证的对象,在这里是Register。



对于BindingResult,您将使用名称:org.springframework.validation。 BindingResult。[ModelAttribute的名称]。



对于您用来验证的对象,您将使用ModelAttribute的名称。



要使用RedirectAttributes,您必须将其添加到您的配置文件中。除此之外,你要告诉Spring使用一些更新的类:

 < mvc:annotation-driven /> 

现在,无论您重定向哪里,都会显示错误

  @RequestMapping(value =/ submit,method = RequestMethod.POST)
public final String submit(@ModelAttribute(register)@Valid final Register register ,最终BindingResult绑定,RedirectAttributes attr,HttpSession会话){

if(binding.hasErrors()){
attr.addFlashAttribute(org.springframework.validation.BindingResult.register,binding );
attr.addFlashAttribute(register,register);
返回redirect:/ register / create;
}

返回redirect:/ register / success;
}


I'm trying to figure out how to "preserve" the BindingResult so it can be used in a subsequent GET via the Spring <form:errors> tag. The reason I want to do this is because of Google App Engine's SSL limitations. I have a form which is displayed via HTTP and the post is to an HTTPS URL. If I only forward rather than redirect then the user would see the https://whatever.appspot.com/my/form URL. I'm trying to avoid this. Any ideas how to approach this?

Below is what I'd like to do, but I only see validation errors when I use return "create".

@RequestMapping(value = "/submit", method = RequestMethod.POST)
public final String submit(
    @ModelAttribute("register") @Valid final Register register,
    final BindingResult binding) {

    if (binding.hasErrors()) {
        return "redirect:/register/create";
    }

    return "redirect:/register/success";
}

解决方案

Since Spring 3.1 you can use RedirectAttributes. Add the attributes that you want to have available before doing the redirect. Add both, the BindingResult and the object that you are using to validate, in this case Register.

For BindingResult you will use the name: "org.springframework.validation.BindingResult.[name of your ModelAttribute]".

For the object that you are using to validate you will use the name of ModelAttribute.

To use RedirectAttributes you have to add this in your config file. Among other things you are telling to Spring to use some newer classes:

<mvc:annotation-driven />

Now the errors will be displayed wherever you are redirecting

@RequestMapping(value = "/submit", method = RequestMethod.POST)
public final String submit(@ModelAttribute("register") @Valid final Register register, final BindingResult binding, RedirectAttributes attr, HttpSession session) {

if (binding.hasErrors()) {
    attr.addFlashAttribute("org.springframework.validation.BindingResult.register", binding);
    attr.addFlashAttribute("register", register);
    return "redirect:/register/create";
}

return "redirect:/register/success";
}

这篇关于Spring - POST后重定向(即使有验证错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆