2

In some circumstances, I have to pass a request to a Wicket page to another Wicket page on the server side, i.e. forward maintaining the URL in the browser address bar, but passing the page parameters to the second page.

Before Wicket 1.5, I could do

public MyPage(PageParameters params) {
    // some logic here to decide whether and where to forward
    setRedirect(false);
    setResponsePage(MyOtherPage.class, params);
}

As setRedirect(boolean) no longer exists, is there a way to achieve a server-side forward in later Wicket versions?

peterp
  • 3,101
  • 3
  • 22
  • 37
  • See also: https://stackoverflow.com/questions/8822137/how-to-redirect-to-another-page-while-keeping-the-original-url – radfast Oct 04 '18 at 22:59

3 Answers3

2

A colleague just found the solution here: http://mail-archives.apache.org/mod_mbox/wicket-users/201203.mbox/%3CCAMomwMr2fkO38E3d9RTk5TEmuf0Vx66F46F8eYs84Bb3bVtPgA@mail.gmail.com%3E

Now it is:

RequestCycle.get().scheduleRequestHandlerAfterCurrent(new RenderPageRequestHandler(new PageProvider(MyOtherPage.class, params), RenderPageRequestHandler.RedirectPolicy.NEVER_REDIRECT));

Scary piece of code... does not look elegant at all, but works.

peterp
  • 3,101
  • 3
  • 22
  • 37
2

On Wicket 6 you can redirect to another page in a simpler way, throwing RestartResponseAtInterceptPageException at any point in your page code:

throw new RestartResponseAtInterceptPageException(WicketPage.class)

It worked fine for me...

s_bighead
  • 1,014
  • 13
  • 24
  • I assume that this would work, however throwing Exceptions is expensive and should not be part of normal program flow... but admittedly, I do not know how expensive above solution is in comparison. As far as I am concerned, the question is out of date anyway :) – peterp Jun 06 '14 at 12:24
  • That's true, maybe is not so bad for performance, but it's really awkward throwing exceptions to forward to another page, they should have kept the old method... – s_bighead Jun 06 '14 at 17:55
  • By using RestartResponseAtInterceptPageException, Wicket will keep track of the initial target page in the session and will send the user back to if if you do "org.apache.wicket.Component.continueToOriginalDestination()". This is appropriate when you want to interrupt the page rendering to send the user to a login page for example, but not if you just want to do a simple redirect. For a simple redirect, org.apache.wicket.RestartResponseException is probably better. – Christophe L Jul 06 '15 at 20:59
1

You should be able to simply do:

throw new RestartResponseException(MyOtherPage.class, params);
Christophe L
  • 13,725
  • 6
  • 33
  • 33