更改默认型号后,为什么我的Wicket Panel不重新渲染? [英] Why doesn't my Wicket Panel rerender after changing the default model?

查看:122
本文介绍了更改默认型号后,为什么我的Wicket Panel不重新渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首次显示带有MessagePanel的页面时,消息和批准链接将完美呈现.当我单击批准链接时,所有业务逻辑均按预期工作,getNextMessage()方法返回适当的对象,但消息面板不会在浏览器的页面上更新.也就是说,邮件正文标签不会更新.

When the page with the MessagePanel first renders, the message and the approve link render perfectly. When I click the approve link, all the business logic works as desired, the getNextMessage() method returns the appropriate object, but the message panel does not update on the page in the browser. That is, the message body Label does not update.

JPAEntityModel扩展了LoadableDetachableModel.

JPAEntityModel extends LoadableDetachableModel.

我想念什么?以及我该如何解决?

What am I missing? And how do I fix it?

public class MessagePanel(String id, IModel<Message> messageModel) extends Panel {
  super(id, messageModel);
  add(new Label("messageText", new PropertyModel<Message>(getModelObject(), Message.BODY_FIELD)));
  add(new IndicatingAjaxFallbackLink<User>("approveLink", new JPAEntityModel<User> (getActiveUser())) {
    @Override
    public void onClick(AjaxRequestTarget target) {
      Message nextMessage = getNextMessage();
      MessagePanel.this.setDefaultModel(new JPAEntityModel<Message>(nextMessage));
      target.add(MessagePanel.this);
    }
});
  setOutputMarkupId(true);
}

推荐答案

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

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.

这篇关于更改默认型号后,为什么我的Wicket Panel不重新渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆