问题
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