错误:找不到分离的模型

时间:2015-11-16 12:48:07

标签: java wicket wicket-1.5 wicket-6 wicket-1.6

使用Wicket 6.19时出现以下错误:

RewriteRule ^files/page/([a-zA-Z0-9\-]+)?$ myPage.html#$1 [NE,NC,L]

代码:

    Caused by: org.apache.wicket.core.util.objects.checker.CheckingObjectOutputStream$ObjectCheckException: Not detached model found!
A problem occurred while checking object with type: com.test.web.components.CurrentUserModel
Field hierarchy is:
   [class=com.test.web.base.pages.HomePage, path=0]
    private java.lang.Object org.apache.wicket.MarkupContainer.children [class=[Ljava.lang.Object;]
      java.lang.Object org.apache.wicket.Component.data[1] [class=com.test.web.HeaderPanel, path=0:headerPanel]
        private java.lang.Object org.apache.wicket.MarkupContainer.children [class=[Ljava.lang.Object;]
          private java.lang.String org.apache.wicket.markup.html.image.resource.LocalizedImageResource.variation[2] [class=com.test.web.LoginStatusPanel, path=0:headerPanel:loginStatusPanel]
            java.lang.Object org.apache.wicket.Component.data [class=com.test.web.components.CurrentUserModel] <----- field that is causing the problem

注意:如果我使用以下内容更新标记为“第1行”的注释中的代码:

@RequireHttps
public class HomePage extends WebPage {
    private HeaderPanel headerPanel;
    private LoginStatusPanel loginStatusPanel;
    private MenuPanel menuPanel;

    public HomePage(String id) {
        super();
        initialize();
    }

    public HomePage(String id, PageParameters parameters) {
        super(parameters);
        initialize();
    }

    @Override
    protected void onInitialize() {
        Request request = RequestCycle.get().getRequest();
        if (request instanceof ServletWebRequest)
        {
            ServletWebRequest wr = (ServletWebRequest) request;
            HttpSession session = wr.getContainerRequest().getSession();
            if (session != null) {
            }
        }
        super.onInitialize();
    }

    public String getApplicationId(){
        return applicationId;
    }

    private void initialize() {
        add(headerPanel = new HeaderPanel("headerPanel"));
        headerPanel.add(loginStatusPanel = new LoginStatusPanel("loginStatusPanel"));
        headerPanel.add(menuPanel = new MenuPanel("menuPanel"));
    }
}

public class HeaderPanel extends Panel {
    private Label headerTitleLbl;

    public HeaderPanel(String id) {
        super(id);
        add(headerTitleLbl=new Label("headerTitle", WebApplication.getAppTitle()));
        headerTitleLbl.setOutputMarkupId(true);
    }
}

public class LoginStatusPanel extends Panel {

    public LoginStatusPanel(String id) {
        super(id, new CurrentUserModel()); // Line 1
    }

    protected void onInitialize() {
        IModel<Authentication> authModel = (IModel<Authentication>) getDefaultModel();
        final Authentication auth = authModel.getObject();

        if (auth != null && auth.isAuthenticated() && !(auth instanceof AnonymousAuthenticationToken))
        {
            Fragment fragment = new Fragment("frag", "loggedInFragment", this);
            Label status;
            fragment.add(status = new Label("status", "Logged in as "));
            fragment.add(new Label("userId", new PropertyModel<String>(authModel, "name")));
            add(fragment);
        }
        else
        {
            Fragment fragment = new Fragment("frag", "loggedOutFragment", this);
            fragment.add(new Label("status", "Logged out"));
            fragment.add(new ExternalLink("login", "/LoginPage", "Log In"));
            add(fragment);
        }
        super.onInitialize();
    }
}

public class MenuPanel extends Panel {
    public MenuPanel(String id) {
        super(id);
        add(new MenuPanel("menu", WebApplication.getMenuList()));
    }
}

public class CurrentUserModel extends LoadableDetachableModel<Authentication> {
    protected Authentication load() {
        return SecurityContextHolder.getContext().getAuthentication();
    }
}

错误未显示,可能是因为它不是LoadableDetachableModel。

是否可以将LoadableDetachableModel的实例传递给Component类构造函数?

我也遇到以下代码的相同错误:

super(id, new Model(SecurityContextHolder.getContext().getAuthentication()));

其中state是以下类的实例:

tree = new NestedTree<TreeItem>("tree", provider, state);

错误如下:

private class TreeItemExpansionModel extends LoadableDetachableModel<Set<TreeItem>>
{
    private static final long serialVersionUID = 1L;
    private final String treePanelId;

    public TreeItemExpansionModel(String treePanelId) {
        this.treePanelId = treePanelId;
    }

    @Override
    protected Set<TreeItem> load() {
        return TreeItemExpansion.get(treePanelId);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof TreeItemExpansionModel) {
            return ((TreeItemExpansionModel) obj).treePanelId.equals(treePanelId);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return treePanelId.hashCode();
    }
}

提前致谢。

2 个答案:

答案 0 :(得分:2)

问题在于序列化检查器与模型生命周期管理的结合。

当您放置LoadableDetachableModel并初始化非Serializable的模型时,Wicket检查程序会抛出一个异常,您尝试序列化不可序列化的数据。这种行为是正确的。

发生这种情况的原因是

  1. 身份验证不可序列化
  2. 你调用getDefaultModel()。get()来加载模型对象,即使它是一个延迟加载模型
  3. 请参阅 NotDetachedModelChecker https://github.com/apache/wicket/blob/build/wicket-6.20.0/wicket-core/src/main/java/org/apache/wicket/core/util/objects/checker/NotDetachedModelChecker.java

    建议修复:

    我。使用 AbstractReadOnlyModel 代替LoadableDetachableModel

        public class CurrentUserModel extends AbstractReadOnlyModel<Authentication> {
           protected Authentication getObject() {
              return SecurityContextHolder.getContext().getAuthentication();
           }
        }
    

    如果你真的需要保留这个值(我认为这不是必需的,因为Spring身份验证上下文在每次尝试时都会返回相同的值),请使用AbstractReadOnlyModel包装LoadableDetachbleModel。

    II。分离模型:在将流控制返回Wicket之前调用 authModel.detach();

    protected void onInitialize() {
        IModel<Authentication> authModel = (IModel<Authentication>) getDefaultModel();
        final Authentication auth = authModel.getObject();
    
        if (auth != null && auth.isAuthenticated() && !(auth instanceof AnonymousAuthenticationToken))
        {
            Fragment fragment = new Fragment("frag", "loggedInFragment", this);
            Label status;
            fragment.add(status = new Label("status", "Logged in as "));
            fragment.add(new Label("userId", new PropertyModel<String>(authModel, "name")));
            add(fragment);
        }
        else
        {
            Fragment fragment = new Fragment("frag", "loggedOutFragment", this);
            fragment.add(new Label("status", "Logged out"));
            fragment.add(new ExternalLink("login", "/LoginPage", "Log In"));
            add(fragment);
        }
        authModel.detach();
        super.onInitialize();
    }
    

答案 1 :(得分:-1)

我知道我在这里提供的信息对于任何人提供适当的解决方案都是不完整的。

我在另一个论坛发布了相同的查询并获得了解决方案。所以我想与分享链接here,以便它对访问此页面的人有用。

由于