Java spring表单未在表单提交时重新填充
当我加载此表单时,countries表由包含240个国家的数据库填充。如果我提交了一些空的必填字段,页面将再次加载,并显示错误消息。但我没有把任何国家列入名单。我使用相同的代码填充GET和POST方法上的列表——请参见下文Java spring表单未在表单提交时重新填充,java,spring,spring-mvc,Java,Spring,Spring Mvc,当我加载此表单时,countries表由包含240个国家的数据库填充。如果我提交了一些空的必填字段,页面将再次加载,并显示错误消息。但我没有把任何国家列入名单。我使用相同的代码填充GET和POST方法上的列表——请参见下文 <form:form commandName="student_personal_info" method="post"> <table> <tr>
<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";
}
国家:
@RequestMapping(value=“student\u personal\u info”,method=RequestMethod.GET)
公共模型和视图显示个人表单(模型和视图模型){
StudentPersonalInfo personalInfo=新建StudentPersonalInfo();
model.addObject(“学生个人信息”,personalInfo);
addObject(“countries”,getCountries());
收益模型;
}//这个很好用
@RequestMapping(value=“student\u personal\u info”,method=RequestMethod.POST)
已提交公共字符串(
@ModelAttribute(“学生个人信息”)@有效的学生个人信息学生个人信息,
BindingResult(结果、模型和视图模型){
addObject(“countries”,getCountries());
if(result.hasErrors()){
logger.info(“根据学生个人信息,有”
+String.valueOf(this.getCountries().size())
+“国家”);//在领事馆上打印240个国家
返回“学生个人信息”;
}
其他的
return“redirect:/display\u program\u of_study.tsegay”;
}
我的所有其他配置都可以正常工作我想如果不返回它,您无法填充
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) { ... }
问题是无法填充
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) {
顺便说一句:ModelAndView
甚至不是中提到的有效参数。它只能是有效的返回类型
在您的特殊情况下,您也可以考虑使用MultEngestAutoTrE方法填充模型:
@ModelAttribute("countries")
public Collection<Country> populateCountries() {
return getCountries();
}
...
@modeldattribute(“国家”)
公共集合人口国家(){
返回getCountries();
}
...