Hello everybody,
Does somebody know how can I dynamically add link address in Wicket?
Hello everybody,
Does somebody know how can I dynamically add link address in Wicket?
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 ExternalLink
s.
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