gpt4 book ai didi

java - Wicket - 更新 PropertyModel 时表单组件丢失引用

转载 作者:行者123 更新时间:2023-12-01 17:52:31 25 4
gpt4 key购买 nike

我有一个带有一些字段和 DropdownChoices 的表单。其中之一是动态填充的:当填充州时,城市下拉列表会更新,直到这里都可以。

动态填充下拉菜单(我使用 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 可以用吗?如果组件的属性模型引用接收到一个新对象,组件会丢失它的引用并需要显式重置?

最佳答案

new PropertyModel(umf, "state") 更改为 new PropertyModel(form.getModel(), "umf.state")城市也是如此。

您面临的问题是,一旦将 umf 传递给 PropertyModel,它就会作为自己的成员字段保存在其中。稍后您更改了 Form 中的引用,但 PropertyModel 仍然指向其成员字段(旧的字段)。

通过传递表单的模型,它变得动态 - 每次 PropertyModel 需要状态时,它都会向表单的模型询问其 modelObject。

关于java - Wicket - 更新 PropertyModel 时表单组件丢失引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48456249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com