gpt4 book ai didi

java - 错误: Not detached model found

转载 作者:行者123 更新时间:2023-12-01 10:53:08 24 4
gpt4 key购买 nike

我在使用 Wicket 6.19 时遇到以下错误:

    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

代码:

@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();
}
}

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

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

错误没有显示,这可能是因为它不是 LoadableDetachableModel。

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

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

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();
}
}

错误如下:

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.TreeItemProvider$TreeItemModel
Field hierarchy is:
[class=com.test.web.admin.pages.HomePage, path=0]
private java.lang.Object org.apache.wicket.MarkupContainer.children [class=[Ljava.lang.Object;]
java.util.List com.test.web.components.WhistlePanel.selectionList[2] [class=com.test.web.NavPanel, path=0:navPanel]
private java.lang.Object org.apache.wicket.MarkupContainer.children [class=org.apache.wicket.extensions.markup.html.repeater.tree.NestedTree, path=0:navPanel:tree]
private java.lang.Object org.apache.wicket.MarkupContainer.children [class=org.apache.wicket.extensions.markup.html.repeater.tree.nested.Subtree, path=0:navPanel:tree:subtree]
private java.lang.Object org.apache.wicket.MarkupContainer.children [class=org.apache.wicket.markup.repeater.RefreshingView, path=0:navPanel:tree:subtree:branches]
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.extensions.markup.html.repeater.tree.nested.BranchItem, path=0:navPanel:tree:subtree:branches:1]
java.lang.Object org.apache.wicket.Component.data [class=com.test.web.components.TreeItemProvider$TreeItemModel] <----- field that is causing the problem

提前致谢。

最佳答案

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

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

发生这种情况的原因是

  1. 身份验证不可序列化
  2. 您调用 getDefaultModel().get() 来加载模型对象,即使它是延迟加载模型

请参阅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

建议修复:

I.使用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();
}

关于java - 错误: Not detached model found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33735795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com