tags:

views:

49

answers:

1

Hello everybody,

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

+2  A: 

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/"&gt;Google&lt;/a&gt;&lt;/div&gt;
<div class="linkItem"><a href="http://www.ebay.com/"&gt;Ebay&lt;/a&gt;&lt;/div&gt;

See

seanizer
I need to add ExternalLink within ListView in the runtime. Your code is useful, but I still can't get the idea how to do that...
sonjafon
OK, see my update
seanizer
Thank you, very much. This code has solved my problems.
sonjafon
then you should accept my answer...
seanizer