Wicket: how to get rid of WicketNotSerializableException?

会有一股神秘感。 提交于 2019-12-11 05:37:46

问题


Ok, so here is the code for the page

public class ViewDocument extends BasePage{

    private List<WeinSyncFileContent> transactions;
    ....

    public ViewDocument(){
        transactions = ....;
        ....
        listContainer.add(listView = new ListView<WeinSyncFileContent>("transactions", transactions){
           @Override
           public void populateItem(final ListItem<WeinSyncFileContent> item)
           {
              ....
           }
        });
    }
}

The page does get displayed but I get errors: Error serializing object class kz.wein.wicket.pages.documents.ViewDocument

and it's complaining about the transactions field: transactions [class=java.util.ArrayList$SubList] <----- field that is not serializable

Also I want to note that objects I am displaying in the list are initially taken from library and are not serializable. Wicket wants serializable objects inside lists so to deal with it I take each object and make it serializable with class like this

public class WeinSyncFileContent extends SyncFileContent implements Serializable{

public WeinSyncFileContent(SyncFileContent obj){
    ... setting fields ...

}

}

so initially I get SyncFileContent objects (that are not serializable) What can I do about the errors?


回答1:


You are getting this error because any field level variables in your Wicket pages will be serialized. So its probably a good idea to not have any non-serializable objects as field level variables. There must be an object in your WeinSyncFileContent that is not serializable which is why you are getting this error.

You may want to instead use models to load your list so something like:

public ViewDocument(){

    ...

    listContainer.add(new ListView<WeinSyncFileContent>(
        "transactions", 
        new LoadableDetachableModel<List<WeinSyncFileContent>>() {
            protected List<WeinSyncFileContent> load() {
                return ...;
            }
        })
    {
       @Override
       public void populateItem(final ListItem<WeinSyncFileContent> item)
       {
          ....
       }
    });
}


来源:https://stackoverflow.com/questions/16855252/wicket-how-to-get-rid-of-wicketnotserializableexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!