Summarization of different ways to redirect or render another response in Apache Wicket.
Some of the sources:
http://stackoverflow.com/questions/3334827/wicket-how-to-redirect-to-another-page
http://stackoverflow.com/questions/5800974/redirect-to-external-non-wicket-page-in-wicket-1-5
http://www.developer.com/java/other/article.php/10936_3673576_6/Wicket-The-First-Steps.htm
Redirection in wicket basically revolves around IRequestHandler implementations.
Martin Grigorov wrote:
IRequestHandler (IRH) is the improved IRequestTarget from 1.4. Read
RequestCycle's javadoc for more information about the scheduling of
IRequestHandlers.
Basically the idea is that the request processing starts with a IRH
that is resolved by the request url, then later during processing a
new IRH can be scheduled to be executed after the current one (e.g.
using setResponsePage() will do that). At the end only the last
scheduled IRH actually writes to the http response.
There are 2 kinds of redirects:
A) In event handlers - links etc.
setResponsePage()
add( new Link("link"){ @Override void onClick(){ setResponsePage( new MyPage( new PageParameters().add("product", product).add("release", release) ); } }
DownloadLink
add( new DownloadLink("download"){
... (TBD)
});
redirectToInterceptPage & continueToOriginalDestination()
First, redirectToInterceptPage() "stores" the current page and redirects to e.g. a sing-in page.
Then, continueToOriginalDestination() redirects back to this stored page, e.g. when user is done with signing-in.
B) During page initialization.
RestartResponseAtInterceptPageException
Renders another page.
Useful if navigated to a page with invalid params (nonexistent id etc.)
ImmediateRedirectException
If actual HTTP redirection is needed.
RequestCycle.scheduleRequestHandlerAfterCurrent(IRequestHandler)
org.apache.wicket.markup.html.pages.RedirectPage
http://wicket.apache.org/apidocs/1.4/org/apache/wicket/markup/html/pages/RedirectPage.html
RedirectToUrlException
import org.apache.wicket.request.flow.RedirectToUrlException; ... throw new RedirectToUrlException( "http://www.facebook.com/login.php?api_key="+ _apiKey + "&v=1.0");
Using HTTP 301 ("Moved Permanently", SEO friendly):
import org.apache.wicket.request.flow.RedirectToUrlException;
import javax.servlet.http.HttpServletResponse;
...
throw new RedirectToUrlException(
"http://www.facebook.com/login.php?api_key="+ _apiKey + "&v=1.0",
HttpServletResponse.SC_MOVED_PERMANENTLY);
Comments