2012-12-31

Wicket does not have exactly a „link with label“ component out of the box.
Remember Wicket tries to hide the HTTP from you (and is quite good at it).
It has a Link, which can react to onClick(), and can contain anything (a simple <span> or a whole <div> with everything inside).

But some people, me included, would like to have a simple link with text, to get:

<a href="...">Text</a>

To get this, you need to prepare such component:

public abstract class LinkWithText extends Link {

    private IModel labelModel;

    public LabelLink(String id, IModel linkModel, IModel labelModel) {
        super(id, linkModel);
        this.labelModel = labelModel;
    }

    public LabelLink(String id, String link, String text) {
      this(id, new Model(link), new Model(text) );
    }

    protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
        replaceComponentTagBody(markupStream, openTag, labelModel.getObject().toString());
    }
}

Then you can have:

<a wicket:id="foo" href="...">Text</a>
this.add( new LinkWithText("foo", "http://pohlidame.cz/", "Insolven�n� rejst��k - Pohl�d�me.cz") );

So far, this is achievable with ExternalLink. Now you can combine it with BookmarkablePageLink and make it integrated with your mapped bookmarkable pages.


Or this, not as good solution, needing you to put the extra <span> inside <a>:

protected class MenuLink extends Fragment
{
  public LinkWithLabel(String id, Class<? extends WebPage> targetPage,
IModel<String> labelModel)
  {
    super(id, "linkWithLabel", MyPage.this);
    BookmarkablePageLink link = new BookmarkablePageLink("link", targetPage);
    link.add(new Label("label", labelModel));
    add(link);
  }
}

In your markup, you'll have:

<wicket:fragment id="menuLink">
  <a href="#" wicket:id="link"><label wicket:id="label"></a>
</wicket:fragment>

This code is off the top of my head, so there may be some compiler
errors, but you get the idea.

0