我仅在某些情况下才会收到此错误。我编码的是注册表,这是基本代码:
@Controller
@SessionAttributes("registrationForm")
@RequestMapping(value = "/registration")
public class RegistrationController
{
@Autowired
private RegistrationValidator registrationValidator;
@RequestMapping(method = RequestMethod.GET)
public String setupForm(Model model) throws Exception
{
return setupForm(model, new RegistrationForm());
}
@RequestMapping(method = RequestMethod.POST)
public String submitForm(@ModelAttribute("registrationForm") RegistrationForm registrationForm,
Model model,
BindingResult result,
SessionStatus sessionStatus) throws Exception
{
registrationValidator.validate(registrationForm, result);
if(result.hasErrors())
{
return setupForm(model, registrationForm);
}
else
{
// Do stuff
sessionStatus.setComplete();
return "redirect:success";
}
}
private String setupForm(Model model, RegistrationForm registrationForm) throws Exception
{
model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
model.addAttribute("registrationForm", registrationForm);
return "registration";
}
表格:
<form:form method="POST" modelAttribute="registrationForm">
...
</form:form>
我遇到的一个非常奇怪的问题是,如果我清除会话数据并直接进入/registration
,整个注册过程就完美无缺。但是当我调用首先在另一个控制器中映射的页面(即主页)时,我会得到标题中指定的错误。
任何人都可以帮助我吗?
谢谢。
答案 0 :(得分:5)
添加此解决了问题:
@ModelAttribute("registrationForm")
public RegistrationForm populateRegistrationForm()
{
return new RegistrationForm();
}