且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更改默认型号后,为什么我的Wicket Panel不重新渲染?

更新时间:2023-02-01 19:30:39

这是因为您没有正确使用模型.

It is because you're not using the model properly.

此行采用面板的模型对象的值(在构建期间设置的值),并使用它来创建组件模型.

This line takes the value of the panel's model object, as it is set during construction, and uses it to create the component model.

add(new Label("messageText", new PropertyModel<Message>(getModelObject(), Message.BODY_FIELD)));

更糟糕的是,当您单击链接时,面板将被赋予新的模型:

To make matters worse, when you click the link, the panel is given a new model:

MessagePanel.this.setDefaultModel(new JPAEntityModel<Message>(nextMessage));

但这显然不会影响标签的模型,因为已经将其设置为引用原始值.

But this obviously doesn't affect the model of the label, as it is already set to refer to the original value.

因此,需要进行两项更改才能使其正常运行.首先,您的标签模型应直接使用面板模型:

So there are two things you need to change to make it work. First off, your label model should use your panel model directly:

new Model<Message>() {
  @Override
  public Message getObject() {
    return MessagePanel.this.getModelObject().getMessage(); //or something similar
  }
}

(注意:上面的代码不一定是***的解决方案,但是它是一个有效的解决方案,演示了如何动态使用模型.)

(Note: the code above isn't necessarily the best solution, but it is a working solution that demonstrates how models can be used dynamically.)

理想情况下,单击链接时不应替换模型,而只需更改模型对象.如果需要自定义模型类(JPAEntityModel),则无论如何都不应在面板构造函数中接受预构建的模型,而只是第一个消息对象.原因是当前的实现从一开始就没有强制使用JPAEntityModel,只是在第一次单击链接之后.

And ideally you shouldn't replace the model when you click the link, just change the model object. If you need a custom model class (JPAEntityModel), you shouldn't be accepting a pre-constructed model in the panel constructor anyway, just the first message object. The reason being the current implementation doesn't enforce the use of JPAEntityModel from the start, only after the first click of the link.