Wicket ListView不令人耳目一新 [英] Wicket ListView not refreshing

查看:165
本文介绍了Wicket ListView不令人耳目一新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache Wicket迈出第一步并遇到以下问题。我有一个 ListView ,它在其条目旁边显示一个删除链接。单击删除链接时,列表项所代表的实体将从数据库中删除,但在我在浏览器中手动重新加载页面之前,列表本身不会更新。

I am taking my first steps with Apache Wicket and ran into the following problem. I have a ListView that displays a "delete" link right next to its entries. When the delete link is clicked, the entity represented by the list item is deleted from the database but the list itself does not get updated until I reload the page manually in the browser.

IModel<List<SampleEntity>> sampleEntityListModel = new LoadableDetachableModel<List<SampleEntity>>() {
        @Override
        protected List<SampleEntity> load() {
            return mSampleEntityBA.findAll();
        }
    };

mListview = new ListView<SampleEntity>("listview", sampleEntityListModel) {
        @Override
        protected void populateItem(final ListItem<SampleEntity> item) {
            item.add(new Label("listlabel", new PropertyModel<String>(item.getModelObject(),
                    "text")));
            item.add(new Link<SampleEntity>("deleteLink", item.getModel()) {
                @Override
                public void onClick() {
                    mSampleEntityBA.delete(item.getModelObject());
                }
            });
        }
};


推荐答案

当onClick调用时,item.getModelObject()从sampleEntityListModel又调用mSampleEntityBA.findAll()。 sampleEntityListModel的模型对象将在请求周期的持续时间内缓存(直到它被分离 - 这通常是你想要的)并且不知道对delete()的调用。

When onClick called, item.getModelObject() pulls from the sampleEntityListModel which in turn calls mSampleEntityBA.findAll(). The model object of sampleEntityListModel will be cached for the duration on the request cycle (until it is detached - which is usually what you want) and is not aware of the call to delete().

为了刷新sampleEntityListModel,在删除之后添加一个sampleEntityListModel.detach()调用(必须使sampleEntityListModel成为final,但这不会导致任何额外的状态被序列化)。这将导致模型在请求周期中稍后呈现列表视图时获取一组新数据。

In order to refresh the sampleEntityListModel, add a sampleEntityListModel.detach() call just after the delete (sampleEntityListModel must be made final, but this will not cause any extra state to be serialized). This will cause the model to fetch a fresh set of data when the list view is rendered later in the request cycle.

这篇关于Wicket ListView不令人耳目一新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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