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

时间:2010-09-04 01:20:59

标签: java wicket

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

1 个答案:

答案 0 :(得分:3)

ExternalLink采用提供链接网址的模型参数。这个模型几乎可以是任何东西。这是一个生成随机链接的方法(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需要ListView ExternalLink s。

这是一个包含链接列表的小组:

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>