UI Redirecting#
This page explains various methods for redirecting from one page to another in you web application.
Please note that we’re talking about redirection as part of a server-side process, such as a form that redirects to a different page based on the user input. If you just want a link in your HTML page, please see UI-Links.
Redirecting to the current page#
Full refresh (keeping the same page instance)#
If you’re in a non-Ajax component, know that handling the request will automatically trigger a full-page refresh. No need for you to do anything.
In the context of an Ajax component you may use this snippet in order to fully refresh the current page:
target.add(getPage());
Or if you want to completely abort your currently executing code, you may throw an exception:
throw new RestartResponseException(getPage());
Redirecting to another instance of the same page#
In some cases, you will want to redirect to another instance of the same page with the same parameters. This is mostly used when a fatal error occurs.
throw new RestartResponseException(getPage().getClass(), getPage().getPageParameters());
For any other redirection (most cases)#
Redirection is mainly done through exceptions. These come in various flavors, depending on your redirection target.
Please note that IPageLinkGenerator
s (see UI-Links) offer methods for easily generating the exception of your choice. This is the recommended way of redirecting.
Here are the main exception types:
RestartResponseException
when you simply want to redirect to another page in your Wicket application.RestartResponseAtInterceptPageException
when you want to redirect to another page which will later trigger another redirection to the current page (mainly used for sign-in pages).RedirectToUrlException
when you want to redirect to an external URL (outside of your Wicket application).
You may also encounter the following patterns in Wicket components or pages. These should be avoided, as they only throw an exception but they do not make it clear, neither to you nor to the compiler. Thus you may end up with dead code after your redirect
call.
// AVOID THIS
redirect(MyPage.class);
// AVOID THIS
redirectToInterceptPage(MyPage.class);
Adding an anchor#
If you want to point to an anchor on the target page, then you must use a RedirectToUrlException
. This feature is built in the IPageLinkGenerator
.