Wicket Guard #4

Simple wrapper for creating bookmarkable page links

Lets say you want to create a parametrizable bookmarkable link like this:

<a href="/cityPage?city=london">London</a>

Creating a dynamic parametrized bookmarkable link needs the following items in wicket:

  • a wicket:id for the anchor’s href
  • a key for the parameter
  • the value for the key
  • a wicket:id for the displaying the text of the link
  • the actual text itself

Here is a simple wrapper method that can do all in one stroke:

	 * Usage:
	 *   createBookmarkablePageLink("linkWicketId", Page.class, "paramKey", $param.value, "textWicketId", $text);
	public static BookmarkablePageLink createBookmarkablePageLink(String _wicketId, Class _pageClass, String _paramKey, String _paramValue, String _wicketHrefTextId, String _hrefText) {
		PageParameters param = new PageParameters();
		param.add(_paramKey, _paramValue);
		BookmarkablePageLink link = new BookmarkablePageLink(_wicketId, _pageClass, param);
		link.add(new Label(_wicketHrefTextId, _hrefText));
		return link;

Here is how the call would look for the above html:

createBookmarkablePageLink("cityLinkWicketId", CityPage.class "city", "london", "cityNameWicketId", "London");

And the corresponding wicket html:

<a wicket:id="cityLinkWicketId" href="#"><span wicket:id="cityNameWicketId">$someCityName</span></a>