Wicket NotSerializableException 填充 ListView

Wicket NotSerializableException populating ListView

我正在开发一个使用 Spring 和 Hibernate 框架的 Web 应用程序。现在我想从我的 Viaggio.java class 一个 JPA 实体中删除 Serializable 接口实现。 此 class 的实体通过 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")));
......

我在此处附加打印的堆栈跟踪:

    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 似乎使用正确。

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

根据经验,您永远不应调用 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 进行日期格式化。