Wicket: how to redirect to another page?

我们两清 提交于 2019-11-29 22:55:11

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.

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

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

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.

you can use

setResponsePage(new RedirectPage("/"));

or

setResponsePage(HomePage.class);

or

throw new RestartResponseException(HomePage.class);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!