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.
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);
}
...
}
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.