Wicket: how to redirect to another page?
Asked Answered
E

5

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.

Elspet answered 26/7, 2010 at 12:40 Comment(0)
G
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.

Grot answered 26/7, 2010 at 23:17 Comment(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.Elspet
@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.Grot
Thanks for the great answer. Simply calling continueToOriginalDestination() pretty much solved my problem too.Heighttopaper
M
23

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);
Mercurate answered 28/6, 2011 at 17:34 Comment(0)
E
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);
   }
   ...
}
Elspet answered 26/7, 2010 at 12:58 Comment(0)
R
3

you can use

setResponsePage(new RedirectPage("/"));

or

setResponsePage(HomePage.class);

or

throw new RestartResponseException(HomePage.class);

Raddy answered 22/5, 2018 at 7:49 Comment(0)
S
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.

Stable answered 8/2, 2015 at 18:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.