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

发布于 2024-11-18 13:30:41 字数 1048 浏览 1 评论 0原文

对于某些 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 可以达到目的,但它看起来确实相当任意!

For some Wicket components, if I call setOutputMarkupId(true) they warn when they are rendered.

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

I'd like to output an id for every component that will actually end up in the HTML in order that I can find them with XPath for testing. How can I predict from class or properties of a Component whether it is sensible to setOutputMarkupId(true)?


More detail - in my Application I'm overriding

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

For a sample of pages, this arbitrary gubbins does the trick, but it does seem pretty arbitrary!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少跟Wǒ拽 2024-11-25 13:30:41

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

Repeater components like DataView and ListView don't have their own markup, but repeat the markup with Items. You can check if the component is instanceof RepeatingView.

This problem is easily shown by the following, wrong, example:

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

Here we don't add the label to the list item, but rather the listview. And that fails. The code should've been:

ListView<Person> people = new ListView<Person>("people", list) {
    protected void onPopulateItem(ListItem<Person> item) {
        item.add(new Label("name", item.getModelObject().getName()));
    }
}
樱花落人离去 2024-11-25 13:30:41

还要忽略自动组件 (Component.isAuto())。
此类组件例如:等。

Also ignore auto-components (Component.isAuto()).
Such components are for example: <wicket:container>, <wicket:enclosure>, etc.

满天都是小星星 2024-11-25 13:30:41

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

您可以通过检查是否绑定到 WicketTag 来找到可能会呈现的组件(未呈现或至少是导致此消息的组件) )。

这可以像这样完成:

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

再次强调:这不是经过尝试的解决方案,只是侵入此编辑器的一些代码行。它被设计为一个指针,而不是一段随时可用的代码。

This is just an idea. I didn't try this ans can't right now...

You could find the components, that will propably be rendered by checking if the are tied to a WicketTag (which are not rendered or at least which are the ones causing this message).

This can be done like this:

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

Again: This is no tried solution, just some lines of code hacked into this editor. It's indended as a pointer not as a ready to use piece of code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文