我一直在用Spring创建一个表单。我曾经有一个页面可以正常工作,所有验证都可以正常工作,并且重定向使我回到带有模型,错误和所有字段的页面。
但是,由于我创建了另一个基于JS的页面,该页面重定向到我的初始申请表。 (如果有任何区别,它将使用sessionstorage)
完整表单控制器中的“调试” sysout完成了所有步骤,并将我重定向回去。这一定与#fig1中的代码有关,但我不知道,我也不知道从哪里开始寻找答案。
我的控制器#fig1
@RequestMapping(value = "/newApplication", method = RequestMethod.GET)
public String retryApplication(Model theModel) {
if (!theModel.containsAttribute("applicant")) {
theModel.addAttribute("applicant", new Applicant());
} else {
Applicant theApplicant = new Applicant();
theModel.addAttribute("applicant", theApplicant);
}
return "application-form";
}
我完整的表单控制器
@RequestMapping(method = RequestMethod.POST)
public String doSendEmail(HttpServletRequest request,@Valid @ModelAttribute("applicant") Applicant Applicant,
BindingResult result, RedirectAttributes redirectAttributes)throws Exception {
//checks form for errors
if(result.hasErrors()) {
System.out.println("found errors");
redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.applicant", result);
System.out.println("adding flashattribute to binding result");
redirectAttributes.addFlashAttribute("applicant", Applicant);
System.out.println("adding flashattribute to applicant");
return "redirect:/newApplication";
}
答案 0 :(得分:0)
绝对正确。在意识到如果要删除所有错误之后我正在创建一个空对象时,问题就出现在图#1中。
这个谜团虽然以前是如何运作的,哈哈。
} else {
Applicant theApplicant = new Applicant();
theModel.addAttribute("applicant", theApplicant);
}