我遇到了一个模型属性的问题,该属性在表单验证后似乎“消失”:
public class QuestionController {
//...
@RequestMapping(value="/get", method=RequestMethod.GET)
public String prepareVoterBean(Model model, @RequestParam String voterID) {
...
VoterBean questions = service.getQuestionBean(voterID);
model.addAttribute("questions", questions);
return "questionPage";
}
@RequestMapping(method=RequestMethod.POST)
public String processSubmit(@Valid VoterBean questions, BindingResult result) {
if (result.hasErrors()) {
logger.info("QuestionController encountered form errors ");
return "questionPage";
}
return "redirect:/ballot/get";
}
以下是 questionPage.jsp,其中 BindingResult 和 bean 名称(“问题”)的普通目标对象都没有出现:
<form:form modelAttribute="questions" method="post">
<fieldset>
<legend>Security Questions</legend>
<p>
<form:label for="birthDate" path="birthDate" cssErrorClass="error"> <fmt:message key="questions.birthDate"/>: </form:label></br>
<form:input path="birthDate" /><form:errors path="birthDate"/>
</p>
//...
使用 HTTP 获取请求可以很好地呈现 questionPage,但是当我提交带有验证错误的表单时,从而触发 processSubmit() 返回到 questionPage,我有 BindingResult 错误。我对自己做错了什么感到非常困惑,因为我的问题 bean 在第一次返回时必须可供 questionPage 使用,但是在带有验证错误的 HTTP POST 请求之后页面突然找不到 bean . 非常感谢您的帮助。谢谢。