当我加载此表单时,国家/地区表格将填入240个国家/地区的数据库中。如果我提交它,带有一些空的必填字段,页面会再次重新加载错误消息。但我没有列出任何国家。我使用相同的代码填充GET和POST方法的列表 - 见下文
<form:form commandName="student_personal_info" method="post">
<table>
<tr>
<td><form:label path="country">Country:</form:label></td>
<td><form:select path="country">
<form:option value="NONE" label=" --Select-- "></form:option>
<form:options items="${countries}"/>
</form:select>
</td>
</tr></table>
</form:form>
@RequestMapping(value = "student_personal_info", method = RequestMethod.GET)
public ModelAndView DisplayPersonalForm(ModelAndView model) {
StudentPersonalInfo personalInfo = new StudentPersonalInfo();
model.addObject("student_personal_info", personalInfo);
model.addObject("countries",getCountries());
return model;
} //this works fine
@RequestMapping(value = "student_personal_info", method = RequestMethod.POST)
public String PersonalFormSubmitted(
@ModelAttribute("student_personal_info") @Valid StudentPersonalInfo student_personal_info,
BindingResult result, ModelAndView model) {
model.addObject("countries", getCountries());
if (result.hasErrors()) {
logger.info("From student personal info, there are "
+ String.valueOf(this.getCountries().size())
+ " Countries"); //This prints 240 countries on the consule
return "student_personal_info";
}
else
return "redirect:/display_program_of_study.tsegay";
}
其他所有配置都可以正常使用
答案 0 :(得分:1)
我猜你不能在没有返回的情况下填充ModelAndView
,所以你需要使用另一个参数类型:
@RequestMapping(value = "student_personal_info", method = RequestMethod.POST)
public String PersonalFormSubmitted(
@ModelAttribute("student_personal_info") @Valid StudentPersonalInfo student_personal_info,
BindingResult result, ModelMap model) { ... }
答案 1 :(得分:1)
问题是您无法填充ModelAndView
参数。
您需要将方法签名更改为ModelMap
而不是ModelAndView
。
@RequestMapping(value = "student_personal_info", method = RequestMethod.POST)
public String PersonalFormSubmitted(
@ModelAttribute("student_personal_info") @Valid StudentPersonalInfo student_personal_info,
BindingResult result, ModelMap model) {
BTW:ModelAndView
甚至不是Spring reference中提到的有效参数。它接缝只是一种有效的返回类型。
在您的特殊情况下,您还可以考虑使用ModelAtttribute方法填充模型:
@ModelAttribute("countries")
public Collection<Country> populateCountries() {
return getCountries();
}
...