填充ListView的Wicket NotSerializableException

时间:2018-02-24 08:46:35

标签: wicket wicket-6

我正在开发一个使用Spring和Hibernate框架的Web应用程序。现在我想从我的Viaggio.java类(JPA实体)中删除Serializable接口实现。 此类的实体通过PageableListView显示在我的Wicket页面中。 当我渲染网页时,我得到一个NotSerializableException。 这是实例化ListView的代码片段:

        final LoadableDetachableModel<List<Viaggio>> viaggiListModel = new LoadableDetachableModel<List<Viaggio>>() {
        @Override
        protected List<Viaggio> load() {
            List<Viaggio> viaggi = utenteService.findAllViaggiForDataAndUsername(dataSelected, utente);
            return viaggi;
        }
    };

    final WebMarkupContainer container = new WebMarkupContainer("tableResult");
    container.setOutputMarkupId(true);
    PageableListView repeating = new PageableListView("repeating", viaggiListModel, 10) {
        @Override
        protected void populateItem(ListItem item) {
            final Viaggio viaggio = (Viaggio) item.getModelObject();
            String date = new SimpleDateFormat("dd-MM-yyyy").format(viaggio.getData());
            item.add(new Label("data", date));
            item.add(new Label("automezzo", new PropertyModel(viaggio, "targaMotrice")));
            item.add(new Label("autista", new PropertyModel(viaggio, "primoAutista")));
            item.add(new EnumLabel("status", new PropertyModel(viaggio, "status")));
......

我在这里附加了打印的stacktrace:

    24/02/2018 09:06:22,629 ERROR -JavaSerializer - Error serializing object class it.loginet.buonicarico.presentation.ViaggioListPage [object=[Page class = it.loginet.buonicarico.presentation.ViaggioListPage, id = 2, render count = 1]]
    org.apache.wicket.core.util.objects.checker.CheckingObjectOutputStream$ObjectCheckException: The object type is not Serializable!
A problem occurred while checking object with type: it.loginet.buonicarico.domain.utente.Viaggio
Field hierarchy is:
  2 [class=it.loginet.buonicarico.presentation.ViaggioListPage, path=2]
    private java.lang.Object org.apache.wicket.MarkupContainer.children [class=[Ljava.lang.Object;]
      private java.lang.Object org.apache.wicket.MarkupContainer.children[5] [class=org.apache.wicket.markup.html.WebMarkupContainer, path=2:tableResult]
        private java.lang.Object org.apache.wicket.MarkupContainer.children [class=[Ljava.lang.Object;]
          private java.lang.Object org.apache.wicket.MarkupContainer.children[0] [class=org.apache.wicket.markup.html.navigation.paging.PagingNavigator, path=2:tableResult:navigator]
            private java.lang.Object org.apache.wicket.MarkupContainer.children [class=[Ljava.lang.Object;]
              private java.lang.Object org.apache.wicket.MarkupContainer.children[0] [class=org.apache.wicket.markup.html.navigation.paging.PagingNavigation, path=2:tableResult:navigator:navigation]
                private java.lang.Object org.apache.wicket.MarkupContainer.children [class=org.apache.wicket.markup.html.list.LoopItem, path=2:tableResult:navigator:navigation:0]
                  private java.lang.Object org.apache.wicket.MarkupContainer.children [class=org.apache.wicket.markup.html.navigation.paging.PagingNavigationLink, path=2:tableResult:navigator:navigation:0:pageLink]
                    protected final org.apache.wicket.markup.html.navigation.paging.IPageable org.apache.wicket.markup.html.navigation.paging.PagingNavigationLink.pageable [class=org.apache.wicket.markup.html.list.PageableListView, path=2:tableResult:repeating]
                      private java.lang.Object org.apache.wicket.MarkupContainer.children [class=org.apache.wicket.markup.html.list.ListItem, path=2:tableResult:repeating:0]
                        private java.lang.Object org.apache.wicket.MarkupContainer.children [class=[Ljava.lang.Object;]
                          java.lang.Object org.apache.wicket.Component.data[1] [class=org.apache.wicket.markup.html.basic.Label, path=2:tableResult:repeating:0:automezzo]
                            java.lang.Object org.apache.wicket.Component.data [class=org.apache.wicket.model.PropertyModel]
                              private java.lang.Object org.apache.wicket.model.ChainingModel.target [class=it.loginet.buonicarico.domain.utente.Viaggio] <----- field that is causing the problem
    at org.apache.wicket.core.util.objects.checker.CheckingObjectOutputStream.internalCheck(CheckingObjectOutputStream.java:362)
    at org.apache.wicket.core.util.objects.checker.CheckingObjectOutputStream.check(CheckingObjectOutputStream.java:341)

它清楚地说明我在PropertyModel中的Viaggio字段是不可序列化的,它最终将手指指向我正在添加到ListItem的“automezzo”标签。

我在哪里错过了这一点? 包装我的Viaggio实体列表的LoadableDetachableModel似乎正确使用。

1 个答案:

答案 0 :(得分:1)

所有标签都引用了PropertyModels,它们本身直接引用您的实体。

根据经验,你不应该致电IModel#getObject()并保持其结果的时间长于所需的时间:

    final WebMarkupContainer container = new WebMarkupContainer("tableResult");
    container.setOutputMarkupId(true);
    PageableListView<Viaggio> repeating = new PageableListView<Viaggio>("repeating", viaggiListModel, 10) {
        @Override
        protected void populateItem(ListItem<Viaggio> item) {
            final IModel<Viaggio> viaggio = item.getModel();

            item.add(new Label("data", new AbstractReadOnlyModel<String>() {
                public String getObject() {
                    return new SimpleDateFormat("dd-MM-yyyy").format(viaggio.getObject().getData())
                }
            });
            item.add(new Label("automezzo", new PropertyModel(viaggio, "targaMotrice")));
            item.add(new Label("autista", new PropertyModel(viaggio, "primoAutista")));
            item.add(new EnumLabel("status", new PropertyModel(viaggio, "status")));
......

您也可以使用PropertyModel而不是AbstractReadOnlyModel,并让Converter执行日期格式化。