Wicket - ComponentNotFoundException:组件已从页面中删除

时间:2018-02-07 10:25:42

标签: java wicket wicket-7

我使用的是Wicket 7,最近我遇到了以下异常:

org.apache.wicket.core.request.handler.ComponentNotFoundException:
Component 'navbar:navbarAvatar:navAvatarPanel:4:navAvatarArea:menuPanel:menuItem:menuLink' has been removed from page.

此错误来自我构建的顶部导航栏。导航栏有一个子菜单(navAvatarPanel),其中包含其他菜单链接(menuLink)。链接将添加到ArrayList中,并使用片段显示在ListView中。

这是我的代码:

NavBarPanel.java

public class NavBarPanel extends Panel {
    public NavBarPanel(String id) {
        super(id);
        /* ... */

        WebMarkupContainer navBarAvatar = new WebMarkupContainer(NavbarAvatar$wicketId) {
            private static final long serialVersionUID = -2447768280463773678L;

            @Override
            public boolean isVisible() {
                return session.isSignedIn();
            }
        };

        List<Panel> avatarMenuItems = new ArrayList<Panel>();
        avatarMenuItems.add(new MenuLink(Logout$wicketId, "Logout", WelcomeWebPage.class, menuModel));
        /* adding additional links at this point ... */

        navBarAvatar.add(new ListView<Panel>("navAvatarPanel", avatarMenuItems) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(ListItem<Panel> item) {
                final Panel panel = item.getModelObject();
                item.add(new MenuFragment(panel, "navAvatarArea"));
            }
        });

        this.add(navBarAvatar);
    }
}

public class MenuFragment extends Fragment {
    private static final long serialVersionUID = 5735397386662232792L;

    public MenuFragment(Panel panel, String area) {
        super(area, "menuFragment", NavBarPanel.this);

        add(panel);
    }
}

NavBarPanel.html

<wicket:panel>
    <!-- ... -->
    <div wicket:id="navbarAvatar" class="navbar-avatar">
        <ul>
            <wicket:container wicket:id="navAvatarPanel">
                <wicket:container wicket:id="navAvatarArea"/>
            </wicket:container>
        </ul>
    </div>
    <!-- ... -->
    <wicket:fragment wicket:id="menuFragment">
        <wicket:container wicket:id="menuPanel"></wicket:container>
    </wicket:fragment>
</wicket:panel>

MenuLink.java

public MenuLink(String markupId, String label, Class<? extends Page> page, MenuModel model) {
    super("menuPanel");
    this.markupId = markupId;
    this.page = page;
    this.model = model;
    this.initComponentTree(label);
}

private void initComponentTree(final String label) {
    WebMarkupContainer menuItem = new WebMarkupContainer("menuItem");

    Link<Void> menuLink = new Link<Void>("menuLink") {
        private static final long serialVersionUID = 1L;

        @Override
        public void onClick() {
            MenuLink.this.menuOnClick(label);
        }
    };

    menuLink.add(new Label("menuLabel", label));
    menuLink.setMarkupId(this.markupId);

    menuItem.add(menuLink);

    this.add(menuItem);
}

MenuLink.html

<wicket:panel>
    <li wicket:id="menuItem">
        <a wicket:id="menuLink">
            <span wicket:id="menuLabel">[label]</span>
        </a>
    </li>
</wicket:panel>

我不知道这个错误来自哪里。我也没有成功找到有关此例外的更多信息。

1 个答案:

答案 0 :(得分:1)

我的猜测是http会话已过期且无法找到菜单项,因为isVisible = session.isSignedIn();整个菜单都不存在。

如果是这种情况,则应改进Wicket以提供更好的异常消息,告知无法找到哪个Component,而不是请求的组件路径。

P.S。建议使用onConfigure() {setVisible(...)}而不是覆盖isVisible(),因为后者在渲染过程中会多次调用,可能会更慢。