Wicket:如何在运行时添加链接地址

发布于 2024-09-16 23:22:53 字数 33 浏览 3 评论 0 原文

有人知道如何在 Wicket 中动态添加链接地址吗?

Does somebody know how can I dynamically add link address in Wicket?

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

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

发布评论

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

评论(1

谁的新欢旧爱 2024-09-23 23:22:53

ExternalLink 采用提供链接 URL 的模型参数。该模型几乎可以是任何东西。这是生成随机链接的一个(LoadableDetachableModel 是动态模型的一种便捷实现):

IModel<String> model=new LoadableDetachableModel<String>() {
    private static final long   serialVersionUID    = 1L;
    @Override
    protected String load() {
        // this class does not really exist
        return LinkRandomizer.getNewRandomUrl();
    }
};
add(new ExternalLink("link", model));

请参阅:


事实证明,OP 需要一个带有 ExternalLinkListView

这是一个带有链接列表的面板:

public class FooPanel extends Panel {

    private static final long   serialVersionUID    = 1L;

    public static class LinkBean{
        private String link;
        private String label;
        public LinkBean(final String link, final String label) {
            this.link = link;
            this.label = label;
        }
        public String getLabel() {
            return this.label;
        }
        public String getLink() {
            return this.link;
        }
        public void setLabel(final String label) {
            this.label = label;
        }
        public void setLink(final String link) {
            this.link = link;
        }


    }

    public FooPanel(final String id) {
        super(id);
        this.add(new ListView<LinkBean>("item", 
                Arrays.asList(
                    new LinkBean("http://www.google.com/","Google"), 
                    new LinkBean("http://www.ebay.com/", "Ebay"))
                ) {

            private static final long   serialVersionUID    = 1L;

            @Override
            protected void populateItem(final ListItem<LinkBean> item) {
                item.add(new ExternalLink("link", item.getModelObject().getLink())
                    .add(new Label("label",item.getModelObject().getLabel()))
                );

            }
        });
    }
}

这是关联的 HTML:

<html><head></head><body>
<wicket:panel>
    <div class="linkItem" wicket:id="item">
        <a href="" wicket:id="link" >
            <wicket:container wicket:id="label" />
        </a>
    </div>
</wicket:panel>
</body></html>

输出将如下所示:

<div class="linkItem"><a href="http://www.google.com/">Google</a></div>
<div class="linkItem"><a href="http://www.ebay.com/">Ebay</a></div>

请参阅

ExternalLink takes a model parameter that supplies a link URL. That model can be pretty much anything. Here's one that generates random links (LoadableDetachableModel is a convenience implementation of a dynamic model):

IModel<String> model=new LoadableDetachableModel<String>() {
    private static final long   serialVersionUID    = 1L;
    @Override
    protected String load() {
        // this class does not really exist
        return LinkRandomizer.getNewRandomUrl();
    }
};
add(new ExternalLink("link", model));

See:


It turns out the OP needs a ListView with ExternalLinks.

Here is a Panel with a list of links:

public class FooPanel extends Panel {

    private static final long   serialVersionUID    = 1L;

    public static class LinkBean{
        private String link;
        private String label;
        public LinkBean(final String link, final String label) {
            this.link = link;
            this.label = label;
        }
        public String getLabel() {
            return this.label;
        }
        public String getLink() {
            return this.link;
        }
        public void setLabel(final String label) {
            this.label = label;
        }
        public void setLink(final String link) {
            this.link = link;
        }


    }

    public FooPanel(final String id) {
        super(id);
        this.add(new ListView<LinkBean>("item", 
                Arrays.asList(
                    new LinkBean("http://www.google.com/","Google"), 
                    new LinkBean("http://www.ebay.com/", "Ebay"))
                ) {

            private static final long   serialVersionUID    = 1L;

            @Override
            protected void populateItem(final ListItem<LinkBean> item) {
                item.add(new ExternalLink("link", item.getModelObject().getLink())
                    .add(new Label("label",item.getModelObject().getLabel()))
                );

            }
        });
    }
}

And here is the associated HTML:

<html><head></head><body>
<wicket:panel>
    <div class="linkItem" wicket:id="item">
        <a href="" wicket:id="link" >
            <wicket:container wicket:id="label" />
        </a>
    </div>
</wicket:panel>
</body></html>

The Output will be something like this:

<div class="linkItem"><a href="http://www.google.com/">Google</a></div>
<div class="linkItem"><a href="http://www.ebay.com/">Ebay</a></div>

See

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