RESTful URL's with Wicket

26 Nov 2010    

This is more a personal rant then anything else.

Anyone who ever used Wicket, will have noticed that it tends to create some funky URL’s. In some cases, the URL will even contain the package structure of the Page we’re building. Obviously we don’t want that.

Wicket provides a very easy method to make the URL’s RESTful. In the WebApplication class of your project, you add lines like these in the init-method:

mountBookmarkablePage("cars", CarPage.class);
mount(new MixedParamUrlCodingStrategy("cardetail", CarDetailPage.class,
                           new String[] {"id"}));

CarPage and CarDetailPage extend WebPage. When passing parameters (id) to the “CarDetailPage”, make sure they are of type org.apache.wicket.PageParameters. The obvious problem with this approach is a large amount of mounts in larger projects.

There is a better solution: wicketstuff-annotation. With this library you can mount the Pages from within the Page

@MountPath(path = "cars")
public class CarPage extends WebPage { ... }

@MountPath(path = "cardetail")
public class CarDetailPage extends WebPage { ... }

Only 1 thing left to do, make sure wicketstuff scans your packages for these annotations. This is done in the init-method of your WebApplication:

new AnnotatedMountScanner().scanPackage("be.daggie.carproject").mount(this);

Just replace “be.daggie.carproject” with your own package.

You can get the library from Maven, all info is over here.

So, if you’re using Wicket, start doing this. If you haven’t used Wicket .. start using it :)