Clean Path Parameters in Java Wicket

I blogged about the Java Tag Cloud for the NixMashup Links App I'm building, but what about displaying the NixMashup Links? In this first of two posts on displaying links from our tag cloud we'll look at how cleanly Wicket handles named parameters in the Url path.

Picking up from our last episode the NixMashup Tag Cloud is below. For purposes of our discussion on Wicket parameters, each link has the HTML format of

<a href='links/java' class='maxTag'>java</a>

where “java” in this example and the CSS class are generated dynamically.  Our tag is “java” and will serve as our Wicket Path Parameter.

Path Parameters Optional

We're going to reuse the same page for Most Recent Links and to display links for the selected Tag. Here's a screenshot of the links displayed for a tag as opposed to all recent links.

Reusing the page is easy and done with only a few lines of code in Wicket because path parameters can be indicated as optional with the “#{tag}” notation. The Java below is the Links Page source where you see the #{tag} listed in the @MountPath value.

Retrieving the tag is as simple as String.valueOf(parameters.get(“tag”)) where we can test its existence with StringUtils.isEmpty() from the Apache Strings library.

@MountPath(value = "/links/#{tag}")
public class LinksPage extends BasePage {

	public LinksPage(PageParameters parameters) {
		super(parameters);

		String tag = String.valueOf(parameters.get("tag"));
		
		String pageTitle = "Most Recent NixMashup Links";
		if (!StringUtils.isEmpty(tag))
		{
			pageTitle = "NixMashup Links for Tag: " + StringUtils.capitalize(tag);
		}
		add(new Label("pageTitle", pageTitle));
		
		add(new NotificationPanel("feedback").hideAfter(Duration.seconds(5)));
		add(new SidePanelLinks("sidepanellinks", "links-listitem"));
		add(new LinksPanel("pagingNavigator", tag));

	}

}

One more point worth mentioning is that the Link List Display logic is in the LinksPanel class. Because Wicket Classes use ordinary Constructors we can pass our tag to the panel where we can perform similar tests to display either all recent links or only the links for the selected tag.

	@SuppressWarnings("serial")
	public LinksPanel(String id, String tag) {
		super(id);

		WebMarkupContainer datacontainer = new WebMarkupContainer("data");
		datacontainer.setOutputMarkupId(true);
		add(datacontainer);

		final SimpleDateFormat postDate = new SimpleDateFormat("MMM d, yyyy");

		List<NixMashupLink> nixMashupLinks = NixMashupLinks.getMostRecentNixMashupLinks();
		if (!StringUtils.isEmpty(tag))
			nixMashupLinks = NixMashupLinks.getNixMashupTagLinks(NixMashupTags.getNixMashupTagLinkIds(tag));

		PageableListView listview = new PageableListView("rows", nixMashupLinks, 20) {
			@Override
			protected void populateItem(ListItem item) {
				NixMashupLink nixMashupLink = (NixMashupLink) item.getModelObject();
				item.add(new Label("linktitle", StringUtils.abbreviate(nixMashupLink.getLinkTitle(), 40)));
				item.add(new Label("postdate", postDate.format(nixMashupLink.getLinkDatetime())));
			}
		};

		datacontainer.add(listview);
		datacontainer.add(new BootstrapPagingNavigator("navigator", listview));
		datacontainer.setVersioned(false);
	}
}

So in total, to reuse both the Links Page and List Display Panel we only had to add maybe 6 lines of code? Not bad!

Posted June 03, 2014 03:12 PM EDT

More Like This Post