Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wicket: how to redirect to another page?

Tags:

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.

like image 836
Mot Avatar asked Jul 26 '10 12:07

Mot


2 Answers

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.

like image 164
Don Roby Avatar answered Sep 24 '22 11:09

Don Roby


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);
like image 39
sother Avatar answered Sep 25 '22 11:09

sother