22

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.

5 Answers 5

29

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.

3
  • 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.
    – Mot
    Jul 27, 2010 at 18:27
  • @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
    Jul 27, 2010 at 22:45
  • Thanks for the great answer. Simply calling continueToOriginalDestination() pretty much solved my problem too.
    – Jonik
    Oct 29, 2010 at 8:54
24

Redirect to a wicket page, using a client-redirect (HTTP 302, the browser's URL changes):

import org.apache.wicket.RestartResponseException;
import org.apache.wicket.request.mapper.parameter.PageParameters;
...
throw new RestartResponseException(
    TargetWicketPage.class, 
    new PageParameters().set("param1", "value1")); 

Redirect to a wicket page, using a server redirect / forward (the browser's URL remains unchanged):

Since Wicket 1.5RC5.1:

import org.apache.wicket.RestartResponseException;
import org.apache.wicket.request.handler.PageProvider;
import org.apache.wicket.request.handler.RenderPageRequestHandler.RedirectPolicy;
import org.apache.wicket.request.mapper.parameter.PageParameters;
...
throw new RestartResponseException(
    new PageProvider(
        TargetWicketPage.class, 
        new PageParameters().set("param1", "value1")), 
    RedirectPolicy.NEVER_REDIRECT));

Before Wicket 1.5RC5.1:

import org.apache.wicket.request.RequestHandlerStack.ReplaceHandlerException;
import org.apache.wicket.request.handler.PageProvider;
import org.apache.wicket.request.handler.RenderPageRequestHandler;
import org.apache.wicket.request.handler.RenderPageRequestHandler.RedirectPolicy;
import org.apache.wicket.request.mapper.parameter.PageParameters;
...
throw new ReplaceHandlerException(
    new RenderPageRequestHandler(
        new PageProvider(
            TargetWicketPage.class, 
            new PageParameters().set("param1", "value1")), 
        RedirectPolicy.NEVER_REDIRECT), 
    true);

Redirect to an URL, using HTTP 302 ("Moved Temporarily"):

import org.apache.wicket.request.flow.RedirectToUrlException;
...
throw new RedirectToUrlException("http://targetURL");

Redirect to an URL, using HTTP 301 ("Moved Permanently", SEO friendly):

import org.apache.wicket.request.flow.RedirectToUrlException;
import javax.servlet.http.HttpServletResponse;
...
throw new RedirectToUrlException("http://targetURL", 
    HttpServletResponse.SC_MOVED_PERMANENTLY);
5

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

you can use

setResponsePage(new RedirectPage("/"));

or

setResponsePage(HomePage.class);

or

throw new RestartResponseException(HomePage.class);

1

I just found

getRequestCycle().setResponsePage(MyOtherPage.class);

which is working at least in wicket 6. It works server-side and rewrites the URL too. Maybe it is a bit faster than using an exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.