我一直试图通过重定向属性传递bindingresult:
我已经引用了,但仍然卡住了。
我有一个获取方法:

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

以及一种后置方法:
@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方法中接收到get方法(重定向之后)的模型中的绑定错误时,我没有看到这些错误-它们似乎消失了。
以下是post方法中的bindingresult对象:
org.springframework.validation.BeanPropertyBindingResult: 1 errors
下面是get方法中显示0个错误的模型条目:
org.springframework.validation.BindingResult.exampleForm=org.springframework.validation.BeanPropertyBindingResult: 0 errors
任何帮助都将不胜感激。

最佳答案

还有一种方法可以解决这个问题。会话属性有助于在请求之间持久化对象,因此以下内容基于它

@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";
}
}

关于spring - 通过RedirectionAttributes传递BindingResult,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15903238/

10-10 15:50