【发布时间】:2011-05-30 22:32:51
【问题描述】:
在我的控制器中:
@Controller
public class UserController {
@RequestMapping(value="/admin/user/id/{id}/update", method=RequestMethod.GET)
public ModelAndView updateUserHandler(@ModelAttribute("userForm") UserForm userForm, @PathVariable String id) {
Map<String, Object> model = new HashMap<String, Object>();
userForm.setCompanyName("The Selected Company");
model.put("userForm", userForm);
List<String> companyNames = new ArrayList<String>();
companyNames.add("First Company Name");
companyNames.add("The Selected Company");
companyNames.add("Last Company Name");
model.put("companyNames", companyNames);
Map<String, Map<String, Object>> modelForView = new HashMap<String, Map<String, Object>>();
modelForView.put("vars", model);
return new ModelAndView("/admin/user/update", modelForView);
}
}
我视图中的选择表单域:
<form:form method="post" action="/admin/user/update.html" modelAttribute="userForm">
<form:select path="companyName" id="companyName" items="${vars.companyNames}" itemValue="id" itemLabel="companyName" />
</form:form>
据我了解,表单支持 bean 将根据表单中的 modelAttribute 属性进行映射。我显然在这里遗漏了一些东西。
【问题讨论】:
-
看来问题与我的设置无关。问题是 itemValue 设置为公司 id 属性,而对我的表单支持 bean 上的公司名称属性进行了比较。所以两者不相等,因此没有设置为选中的项目。
标签: spring forms spring-mvc