Wicket:如何摆脱 WicketNotSerializableException? [英] Wicket: how to get rid of WicketNotSerializableException?
问题描述
好的,这是页面的代码
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)
{
....
}
});
}
}
页面确实显示了,但出现错误:序列化对象类 kz.wein.wicket.pages.documents.ViewDocument 时出错
The page does get displayed but I get errors: Error serializing object class kz.wein.wicket.pages.documents.ViewDocument
它在抱怨交易字段:交易 [class=java.util.ArrayList$SubList] <----- 不可序列化的字段
and it's complaining about the transactions field: transactions [class=java.util.ArrayList$SubList] <----- field that is not serializable
我还要注意,我在列表中显示的对象最初是从库中获取的,并且不可序列化.Wicket 想要列表中的可序列化对象,因此为了处理它,我获取每个对象并使用这样的类使其可序列化
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 ...
}
}
所以最初我得到 SyncFileContent 对象(不可序列化)对于这些错误,我该怎么办?
so initially I get SyncFileContent objects (that are not serializable) What can I do about the errors?
推荐答案
您收到此错误是因为 Wicket 页面中的任何字段级变量都将被序列化.因此,最好不要将任何不可序列化的对象作为字段级变量.您的 WeinSyncFileContent 中必须有一个不可序列化的对象,这就是您收到此错误的原因.
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)
{
....
}
});
}
这篇关于Wicket:如何摆脱 WicketNotSerializableException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!