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>

Wicket Guard #3

ListView vs DataView/ListProvider

One thing to remember when using DataView instead of ListView is that the DataView does not have a setListProvider(), neither the ListProvider has a setList(). In other words, once these objects are initialized with the list, there is no way to set another new List object. ListView has a setList() which can be called for eg during a onSubmit() to update the list.

The correct way to handle that is, on refreshing, clear out the old items from the list and add new items. When the page/form is displayed again, the new values will be used.

For eg

  List list = new ArrayList();
  //list.add(...)
  DataView dv = new DataView("dataView", new ListProvider("listProvider", list));

  protected void onSubmit() {
    list.clear();
    list.add(...);
    //thats it, no need, (rather cannot) set the list to ListProvider again
  }

So when querying list from say Spring’s JdbcTemplate queryList() methods, be aware that it returns a new List object, which the ListProvider will not pickup. You can simply do a list.clear(), followed by list.addAll(newList);