views:

262

answers:

2

How do I redirect to another page using Wicket? IIRC, some exception has to be thrown in the constructor, but I don't remember which one. Thanks in advance.

+2  A: 

A quick search for all *Exception.java files in wicket revealed it. One has to throw a RestartResponseAtInterceptPageException:

public MyPage() {
   ...
   if (redirect) {
       throw new RestartResponseAtInterceptPageException(targetPage);
   }
   ...
}
mklhmnn
+6  A: 

Throwing a RestartResponseAtInterceptPageException will do it, as you noted in your own answer, but that's really part of a system for allowing a redirect with an eventual continuation at the current page (frequently part of an authorization process). If that's not your situation, but you still have to do something that interrupts processing, it might be better to throw a RestartResponseException.

The principal usage that I know of for RestartResponseAtInterceptPageException is in the "redirect to login page" process. If you're using role-based authentication, an implementation of IAuthorizationStrategy on determining that you're not logged in will signal a configured IUnauthorizedComponentInstantiationListener, typically the AuthenticatedWebApplication which throws this exception if you're not logged in, with a redirect to a configured login page. (If you're logged in but unauthorized, something else happens...).

The actual redirect is done by the PageMap, which also in this case remembers the page you were trying to go to. After a successful login, the login page can ask to send you to the page you were trying for originally by calling continueToOriginalDestination(), which is a method in Component and retrieves the remembered page from the PageMap.

There's some good example code for this authentication process, but the exception and intercept are hiding behind the scenes somewhat.

Don Roby
Sorry, I don't understand what you mean with "eventual continuation at the current page". Could you please elaborate a little bit? Thanks in advance.
mklhmnn
@mklhmnn: I've added some notes about a common usage and a bit of how it works. It's convoluted, so I'm not sure how much this helps.
Don Roby
Thanks for the detailed explanation.
mklhmnn
Thanks for the great answer. Simply calling continueToOriginalDestination() pretty much solved my problem too.
Jonik