1

In my project, there are additional (non-wicket) applications, which need to know the URL representation of some domain objects (e.g. in order to write a link like http://mydomain.com/user/someUserName/ into a notification email).

Now I'd like to create a spring bean in my wicket module, exposing the URLs I need without having a running wicket context, in order to make the other application depend on the wicket module, e.g. offering a method public String getUrlForUser(User u) returning "/user/someUserName/".

I've been stalking around the web and through the wicket source for a complete workday now, and did not find a way to retrieve the URL for a given PageClass and PageParameters without a current RequestCycle.

Any ideas how I could achieve this? Actually, all the information I need is somehow stored by my WebApplication, in which I define mount points and page classes.

peterp
  • 3,101
  • 3
  • 22
  • 37
  • Is [this](http://stackoverflow.com/questions/7068429/wicket-how-to-render-page-programmatically-and-get-result-as-string) similar enough to what you need? – biziclop Oct 22 '12 at 18:00
  • Thanks, but no, not at all. I need the URL, not the rendered markup. – peterp Oct 26 '12 at 13:36

1 Answers1

2

Update: Because the code below caused problems under certain circumstances (in our case, being executed subsequently by a quarz scheduled job), I dived a bit deeper and finally found a more light-weight solution.

Pros:

  • No need to construct and run an instance of the WebApplication
  • No need to mock a ServletContext
  • Works completely independent of web application container

Contra (or not, depends on how you look at it):

  • Need to extract the actual mounting from your WebApplication class and encapsulate it in another class, which can then be used by standalone processes. You can no longer use WebApplication's convenient mountPage() method then, but you can easily build your own convenience implementation, just have a look at the wicket sources.

(Personally, I have never been happy with all the mount configuration making up 95% of my WebApplication class, so it felt good to finally extract it somewhere else.)

I cannot post the actual code, but having a look at this piece of code will give you an idea how you should mount your pages and how to get hold of the URL afterwards:

    CompoundRequestMapper rm = new CompoundRequestMapper();
    // mounting the pages
    rm.add(new MountedMapper("mypage",MyPage.class));
    // ... mount other pages ...

    // create URL from page class and parameters
    Class<? extends IRequestablePage> pageClass = MyPage.class;
    PageParameters pp = new PageParameters();
    pp.add("param1","value1");
    IRequestHandler handler = new BookmarkablePageRequestHandler(new PageProvider(MyPage.class, pp));
    Url url = rm.mapHandler(handler);

Original solution below:

After deep-diving into the intestines of the wicket sources, I was able to glue together this piece of code

IRequestMapper rm = MyWebApplication.get().getRootRequestMapper();
IRequestHandler handler = new BookmarkablePageRequestHandler(new PageProvider(pageClass, parameters));
Url url = rm.mapHandler(handler);

It works without a current RequestCycle, but still needs to have MyWebApplication running.

However, from Wicket's internal test classes, I have put the following together to construct a dummy instance of MyWebApplication:

MyWebApplication dummy = new MyWebApplication();
dummy.setName("test-app");
dummy.setServletContext(new MockServletContext(dummy, ""));
ThreadContext.setApplication(dummy);
dummy.initApplication();
peterp
  • 3,101
  • 3
  • 22
  • 37