dav*_*ooh 2 java spring controller spring-mvc
我正在使用Spring MVC开发测试应用程序.我有Person
班级和Group
班级.每个Person
对象都引用一个Group
对象.
现在我实现了一个显示Person数据并允许编辑的jsp.在我的表单中,我选择了一个选择控件来选择皮尔逊的组:
<sf:select path="group">
<sf:options items="${groupList}" itemLabel="name" itemValue="id" />
</sf:select>
Run Code Online (Sandbox Code Playgroud)
它在我加载页面时显示正确的组,但我无法保存更改,因为在控制器中我只获得表示组ID的字符串.
所以,我的问题是:如何Group
在控制器中获取对象而不是其id?
更新 这里我的控制器代码:
@RequestMapping(value = "/details", params = "save", method = RequestMethod.POST)
public String save(@ModelAttribute("person") Person p,
BindingResult result) {
this.personManager.savePerson(p);
return "redirect:/people/details?id=" + p.getId();
}
Run Code Online (Sandbox Code Playgroud)
通过扩展PropertyEditorSupport创建您自己的GroupEditor(将正确填充组对象实例).然后在控制器中绑定它:
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Group.class, new GroupEditor(groupService));
}
Run Code Online (Sandbox Code Playgroud)
你的实际编辑可能看起来像这样:
public class GroupEditor extends PropertyEditorSupport{
private final GroupService groupService;
public GroupEditor(GroupService groupService){
this.groupService= groupService;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
Group group = groupService.getById(Integer.parseInt(text));
setValue(group);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5441 次 |
最近记录: |