我正在制作spring mvc应用程序并使用速度模板进行视图。有表格用于填充数据,在此表格中我使用#springFormSingleSelect.Form在数据有效时工作正常,但当表格由无效数据填充时,如上所述有错误消息,但#springFormSingleSelect 为空。这是我的观点:
<form action="saveEmployee" method="POST">
#springBind("newEmployee")
<a href="listEmployees">#springMessage("label.back")</a>
<p>
<table>
<tr>
<td>#springMessage("label.firstName")</td>
<td>#springFormInput("newEmployee.firstName" "")</td>
<td><font color="red">#springShowErrors(" " "")</font></td>
</tr>
<tr>
<td>#springMessage("label.lastName")</td>
<td>#springFormInput("newEmployee.lastName" "")</td>
<td><font color="red">#springShowErrors(" " "")</font></td>
</tr>
<tr>
<td>#springMessage("label.salary")</td>
<td>#springFormInput("newEmployee.salary" "")</td>
<td><font color="red">#springShowErrors(" " "")</font></td>
</tr>
<tr>
<td>#springMessage("label.birthdate")</td>
<td>#springFormInput("newEmployee.birthday" "")</td>
<td><font color="red">#springShowErrors(" " "")</font></td>
</tr>
<tr>
<td>#springMessage("label.departament")</td>
<td>#springFormSingleSelect("newEmployee.departamentId" $departamentsMap "")</td>
</tr>
</table>
<input type="submit" value="#springMessage("label.submit")">
</form>
这是视图控制器的一部分:
@RequestMapping(value="/saveEmployee", method= RequestMethod.GET)
public ModelAndView newuserForm(){
ModelAndView model = new ModelAndView("newEmployee");
Employee empl = new Employee();
Map departamentsMap = new TreeMap();
List<Departament> departamentsList = service.listDepartaments();
//for singleselect of departaments
for(int i=0; i<departamentsList.size(); i++){
Departament dep = departamentsList.get(i);
departamentsMap.put(dep.getDepartamentId(),dep.getTitle() );
}
model.addObject("departamentsMap",departamentsMap);
model.getModelMap().put("newEmployee", empl);
return model;
}
@RequestMapping(value="/saveEmployee", method=RequestMethod.POST)
public String create(@ModelAttribute("newEmployee")Employee empl, BindingResult result){
employeeValidator.validate(empl, result);
if(result.hasErrors()){
return "newEmployee";
}
service.saveEmployee(empl);
return "redirect:/listEmployees";
}
也许有人知道这种行为的原因?