我有两个这样的类:
public class A {
B b;
}
public class B {
String id;
}我有一个表单来修改我的对象A和它的subs对象:
它发送
b.id=XXXXX
我想做的是
a.setB(BDao.findbyId(b.id));我想在DB中查找B对象,并在对象A上设置它,而不是设置A.b对象的id属性
在自动绑定之后,我可以在控制器中手动完成:
a.setB(bDao.findbyId(a.getB().getId));但是有没有可能用一个自定义编辑器来做到这一点?
谢谢!
发布于 2014-03-14 22:58:14
我认为这是可能的。你必须编写一个自定义的初始化绑定器,它基本上是一个'PropertyEditor',你可以在其中扩展'PropertyEditorSupport‘类。它看起来像这样:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(MyClass.class, new MyCustomePropertyEditor());
}其中你的MyCustomePropertyEditor看起来像这样:
public class MyCustomePropertyEditorextends PropertyEditorSupport {
public void setAsText(String text) {
MyClass mc = service.doSomethingToLookUpMyClass(text);
setValue(mc);
}
}在JSP/View中,您需要更改标记,以便它们设置您的对象引用,而不是引用的id。这显然是即兴和记忆写出来的,所以里程可能会有所不同。您可能还想考虑查看自定义验证器,我相信您也可以通过init绑定器进行设置。文档是here。
https://stackoverflow.com/questions/22407864
复制相似问题