【发布时间】:2018-01-26 05:34:06
【问题描述】:
我有一个包含一些字段和 DropdownChoices 的表单。其中一个是动态填充的:当 State 被填充时,City 下拉菜单会更新,直到这里都可以。
动态填充的下拉菜单(我使用 hibernate 作为 ORM):
// in my form constructor
// umf is my model
umf = new Umf();
DropDownChoice stateChoice = new DropDownChoice<State>(
"states",
new PropertyModel(umf, "state"),
em.createQuery("from State e").getResultList(),
new ChoiceRenderer<State>("name")
);
DropDownChoice citiesChoice = new DropDownChoice<City>(
"cities",
new PropertyModel(umf, "city"),
new ArrayList<City>(),
new ChoiceRenderer<>("name")
);
当我尝试清除表单和我的模型以让表单为其他提交做好准备时,问题出现在第一次表单提交之后(发生正常)。
第一个问题是在 onSubmit 方法中,在将对象持久化到数据库后,我为我的模型设置了一个新对象:umf = new Umf();,以便准备好持久化一个新的 umf。在此之后,组件似乎丢失了 umf 引用。
定义下拉状态模型的行:new PropertyModel(umf, "state") 不再起作用,因为即使我更改下拉列表中的状态,umf.state PropertyModel 也未更新(始终为 0),因此未填充城市下拉列表。
// right after statesChoice and citiesChoice declaration
statesChoice.add(new AjaxFormComponentUpdatingBehavior("change") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
citiesChoice.setChoices(
em.createQuery("from City m where m.state.id = :state_id")
.setParameter("state_id", umf.getState().getId())
.getResultList()
);
target.add(cititesChoice);
}
});
Wicket 是这样工作的吗?如果组件的属性模型引用接收到一个新对象,组件会丢失它的引用并需要显式重置?
【问题讨论】:
标签: java forms hibernate wicket dropdownchoice