通过RedirectionAttributes传递BindingResult [英] Passing BindingResult through RedirectionAttributes

查看:160
本文介绍了通过RedirectionAttributes传递BindingResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在尝试通过RedirectionAttributes传递BindingResult:

I am stuck trying to passing the BindingResult through RedirectionAttributes:

我引用了 Spring-POST后重定向(即使存在验证错误) ),但仍然被卡住.

I have referenced Spring - Redirect after POST (even with validation errors), but am still stuck.

我有一个GET方法:

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test(@ModelAttribute("exampleForm") final ExampleForm exampleForm, final Model model)
{
    return "test";
}

和POST方法:

@RequestMapping(value = "/doSomething", method = RequestMethod.POST)
public String doSomething(@Valid @ModelAttribute("exampleForm") final ExampleForm exampleForm, final BindingResult bindingResult, final RedirectAttributes redirectAttributes)
{
    if (bindingResult.hasErrors())
    {
        redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.exampleForm", bindingResult);
        redirectAttributes.addFlashAttribute("exampleForm", exampleForm);
        return "redirect:/test";
    }
}

但是,当我在POST方法中收到绑定错误时(在重定向之后),我没有看到它们的绑定错误-它们似乎消失了.

However, I am not seeing binding errors in the model of the GET method (after redirect) when I receive them in the POST method - they seem to disappear.

这是POST方法中的bindingResult对象: org.springframework.validation.BeanPropertyBindingResult: 1 errors

Here is the bindingResult object in the POST method: org.springframework.validation.BeanPropertyBindingResult: 1 errors

这是GET方法中的模型条目,显示0个错误: org.springframework.validation.BindingResult.exampleForm=org.springframework.validation.BeanPropertyBindingResult: 0 errors

Here is the model entry in the GET method showing 0 errors: org.springframework.validation.BindingResult.exampleForm=org.springframework.validation.BeanPropertyBindingResult: 0 errors

任何帮助将不胜感激.

推荐答案

可以解决此问题的另一种方法.会话属性有助于在请求之间保留对象,因此基于它的以下内容

One more approach that may resolve the issue. Session attributes help to persist objects between requests, so the following based on it

@Controller
@SessionAttributes(
{ "exampleForm" })
public class HomeController
{

@ModelAttribute("exampleForm")
public ExampleForm getExampleForm()
{
    return new ExampleForm();
}

@RequestMapping(value = "/myform", method = RequestMethod.POST)
public String proccessForm(@Valid @ModelAttribute("exampleForm") 
        final ExampleForm form, final BindingResult result,
        final SessionStatus sessionStatus)
{
    if (result.hasErrors())
    {
        return "redirect:/myform";
    }
    sessionStatus.setComplete();
    return "redirect:/complete";
}

@RequestMapping(value = "/myform", method = RequestMethod.GET)
public String showForm(final Model model)
{
    return "form";
}

@RequestMapping(value = "/complete", method = RequestMethod.GET)
public String showSomething(final Model model)
{
    return "complete";
}
}

这篇关于通过RedirectionAttributes传递BindingResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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