在 Wicket 中处理模型对象集合的正确方法是什么? [英] What is the correct way to handle collections of model objects in Wicket?

查看:29
本文介绍了在 Wicket 中处理模型对象集合的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关处理 Wicket 中对象集合的最佳方式的一些帮助或指导,这些方式不会对会话大小产生破坏性影响.显然,使用 Wicket 的任何 IModel 类包装对象是理想的,但在处理对象集合(例如搜索结果集合)时,最好的方法是什么?

I am looking for some assistance or guidance on the best way to handle collections of objects in Wicket that won't have a devastating impact on session size. Obviously wrapping an object with any of Wicket's IModel classes is ideal, but what is the best way to do that when working with a collection of objects (e.g. a collection of search results).

我在处理单个对象时成功使用 LoadableDetachableModel,但在关闭 Tomcat 时似乎间歇性地收到 java.io.NotSerializableException.最初,我认为我是安全的,但抛出的异常表明并非如此.

I have had success using LoadableDetachableModel when working with an individual object, but I appear to be getting a java.io.NotSerializableException intermittently when shutting down Tomcat. Initially, I thought I was safe but the exception being thrown suggests otherwise.

这是代码(为简洁起见进行了编辑):

This is the code (edited for brevity):

public class CandidateSearch extends BasicPage {

private static final long serialVersionUID = 1L;
private CandidateService service = new CandidateService();

public CandidateSearch() {
    ListView<Candidate> listView = new ListView<Candidate>("candidates", service.search()){

        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(ListItem<Candidate> item) {
            Candidate candidate = (Candidate) item.getModelObject();

            PageParameters pars = new PageParameters();
            pars.add("id", candidate.getId());
            Link<String> candidateLink = new BookmarkablePageLink<String>("candidateLink", CandidateDetails.class, pars);

            candidateLink.add(new Label("candidateId", "ID-" + new Long(candidate.getId()).toString()));

            item.add(candidateLink);
            item.add(new Label("name", candidate.getFirstName() + " " + candidate.getLastName()));
            item.add(new Label("location", candidate.getCity() + ", " + candidate.getState()));
        }

    };

    add(listView);

}

}

注意:service.search 返回一个类型为 Candidate 的 java.util.List.

NOTE: service.search returns an java.util.List typed as Candidate.

推荐答案

当你像这样构造 ListView 时,你的 Candidate 对象将是不可分离的......那就是为什么你会得到 java.io.NotSerializableException.

When you construct the ListView like that, your Candidate objects will not be detachable...that's why you're getting the java.io.NotSerializableException.

我不确定这是否是最佳实践,但我的策略是将对象列表转换为列表的 LoadableDetachableModel.我有一个这样的实用方法:

I'm not sure if this is best practices or not, but my strategy is to convert the list of objects into a LoadableDetachableModel of a List. I have a utility method like this:

public static <T> IModel<? extends List<T>> convertToListViewModel(List<T> objects) {

    final Class<? extends List> listClass = objects.getClass();

    // NOTE: you will need to implement the toLoadableDetachableModels method
    List<IModel<T>> asModels = toLoadableDetachableModels(objects);

    return new LoadableDetachableModel<List<T>>() {
        @Override
        protected List<T> load() {
            List<T> results = ClassUtils.newInstance(listClass);
            for(IModel<T> model : asModels) {
                results.add(model.getObject());
            }
            return results;
        }
    };
}

您可以使用此方法来包装 service.search() 的结果,然后不仅应该消除该错误,而且您的对象将需要更少的会话存储空间.

You could use this method to wrap the results of service.search(), and then not only should you should be rid of that error, but your objects will require much less session storage space.

这篇关于在 Wicket 中处理模型对象集合的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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