Andreas Krueger Andreas Krueger - 6 months ago 38
Java Question

How to automatically redirect from one page to another within a few seconds

The idea is to display a

that is visible for a few seconds and automatically redirects to the
, when the web session expires.

By the following code the
displays with a bookmarkable link to the
on it.


Your session expired, log in anew by clicking
<a wicket:id="lnk-home-page" href="#"> here</a>

final Application app = Session.get().getApplication();
BookmarkablePageLink<? extends Page> lnkHomePage = new BookmarkablePageLink<? extends Page>("lnk-home-page", app.getHomePage());

How to code in Wicket that the
, when displayed, automatically redirects to
after a configurable number of seconds?


A better solution than the RedirectPage is a custom behaviour. The obvious problem with RedirectPage is that you can't use a common base class for the layout of the page.

public class RedirectBehavior extends Behavior {

    private final Class<? extends Page> page;
    private final int redirectInSeconds;

    public RedirectBehavior(Class<? extends Page> page, int redirectInSeconds) { = page;
        this.redirectInSeconds = redirectInSeconds;

    public void renderHead(Component component, IHeaderResponse response) {
        response.renderString(String.format("<meta http-equiv='refresh' content='%d;URL=%s' />", redirectInSeconds,
                RequestCycle.get().urlFor(page, null)));

This way you can pass the return value from getHomePage() directly - no need for newInstance():

public class PageExpiredPage extends YourBasePage {
    public PageExpiredPage () {
        add(new RedirectBehavior(Application.get().getHomePage(), 5));