Wednesday, January 11, 2012

Wicket 1.5 mountPage with optional parameters

I am using Wicket 1.5 as web layer for application. I needed a page that could take parameter but it should be optional.

So I have an URL like this http://example.com/search/keyword/1, it has 2 parameters search (keyword) and page (1), but I want page to be optional, so that URL like example.com/search/keyword should be mapped to the same page.

I tried to map it with mountPage(“/search/${keyword}/${page}”, SearchPage.class);

But the problem was that page parameter was mandatory.




Solution was found here : wicketinaction.com, a blog by Martijn Dashorst
Martijn Dashorst says:

One more cool thing is that you can have optional indexed named parameters. The difference with mandatory ones is that they use hash sign instead of dollar sign – #{optional}.

A mapping like mountPage(“/mount/path/${named1}/#{named2}”, MyPage.class); will match for both requests http://host/mount/path/value1 and http://host/mount/path/value1/value2.


So I used # instead of dollar! mountPage(“/search/${keyword}/#{page}”, SearchPage.class); and it works!





2 comments:

  1. hello one question,

    my map is

    mountPage("/page/#{code}/#{name}", Page.class);

    but when I click on the link
    localhost/page/10/toy?2
    wicket add also one parameter like a counter, when I refresh the page I have
    localhost/page/10/toy?3
    why?

    ReplyDelete
  2. Hey Thanks for sharing...
    Finally i was able to make some cool URL and they help me in creating dynamic Web Pages.

    ReplyDelete