Wicket: How to add link address in runtime
Does somebody know how can I dynamica开发者_运维百科lly 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:
- Working with Wicket Models
- Using Wicket Labels and Links
- ExternalLink API docs
- LoadableDetachableModel API docs
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
- ListView and other repeaters (an ancient tutorial, careful: this relates to the older non-generic version of ListView in 1.3.x)
- Wicket Examples: Repeater Views (ListView is not covered, but the handling is pretty identical)
- ListView Api docs
- Wicket in Action (A very good book about Wicket you should consider buying)
精彩评论