如何预测哪些Wicket组件将在最终页面中呈现其标签?

时间:2011-07-01 12:30:10

标签: wicket

对于某些Wicket组件,如果我调用setOutputMarkupId(true),它们会在呈现时发出警告。

Markup id set on a component that is usually not rendered into markup.

我想为每个实际上都会在HTML中输出的组件输出一个id,以便我可以使用XPath找到它们进行测试。如何从Component的类别或属性预测setOutputMarkupId(true)是否合理?


更多细节 - 在我的Application我压倒

protected void init() {
    super.init();
    addComponentInstantiationListener(new IComponentInstantiationListener() {
        public void onInstantiation(Component component) {
            if (!(component instanceof WebMarkupContainer)) return;
            if (component instanceof WebMarkupContainerWithAssociatedMarkup) return;
            if (component instanceof Border.BorderBodyContainer) return;
            WebMarkupContainer container = (WebMarkupContainer) component;
            if (container.isTransparentResolver()) return;

            component.setOutputMarkupId(true);
        }});

对于一个页面样本,这个任意的gubbins可以解决这个问题,但它看起来确实很随意!

3 个答案:

答案 0 :(得分:1)

DataViewListView等转发器组件没有自己的标记,但是使用Item重复标记。您可以检查该组件是否为instanceof RepeatingView

以下错误的示例很容易显示此问题:

ListView<Person> people = new ListView<Person>("people", list) {
    protected void onPopulateItem(ListItem<Person> item) {
        add(new Label("name", item.getModelObject().getName()));
    }
}

这里我们不会将标签添加到列表项,而是列表视图。而那失败了。代码应该是:

ListView<Person> people = new ListView<Person>("people", list) {
    protected void onPopulateItem(ListItem<Person> item) {
        item.add(new Label("name", item.getModelObject().getName()));
    }
}

答案 1 :(得分:1)

还要忽略自动组件(Component.isAuto())。 这些组件例如是:<wicket:container><wicket:enclosure>

答案 2 :(得分:1)

这只是一个想法。我没有尝试这个ans现在不能......

您可以通过检查是否与WicketTag(未呈现或至少是导致此消息的WicketTag)相关联来找到可以呈现的组件。

这可以这样做:

boolean isGoingToBeRendered (Component component) {
    MarkupStream stream = new MarkupStream(component.getMarkup());
    ComponentTag tag = stream.getTag();
    return (!tag instanceof WicketTag);
}

再次说明:这是没有尝试过的解决方案,只是在这个编辑器中被攻击了一些代码。它被指定为一个指针而不是一个准备好使用的代码。

相关问题