Why RestartResponseException in exception hanler doesn't redirect to other page?

Viewed 83

I use RestartResponseException in exception handler (IRequestCycleListener) for redirection on login page and it doesn't work. I see exception in logs but no changes in UI.

        @Override
        public IRequestHandler onException(RequestCycle cycle, Exception ex) {
            if (couldNotLock) {
                getSessionStore().invalidate(cycle.getRequest());
                throw new RestartResponseException(LoginPage.class);
            }

            return null;
        }
3 Answers

Even with your extra information in the question's comment it is still not clear what kind of problem you face.

I will try to guess: your LoginPage checks whether the user is already logged in and just redirects to the landing/home page of your application.

To fix that you need to invalidate the whole Session:

@Override
public IRequestHandler onException(RequestCycle cycle, Exception ex) {
    if (couldNotLock) {
       Session.get().invalidate();    // !!!
       throw new RestartResponseException(LoginPage.class);
    }

    return null;
}

Try to throw new RestartResponseAtInterceptPageException(LoginPage.class) instead of RestartResponseException

If nothing else helps You can modify the page's URL with javascript.

@Override
public void renderHead(IHeaderResponse response) {
    super.renderHead(response);
    response.render(JavaScriptHeaderItem.forScript("window.history.pushState(\"object or string\", \"" + pageTitle + "\", \"" + pageUrl + "\")"));
}

for AJAX:

    AjaxRequestTarget target = optional.get();
    target.prependJavaScript("window.history.pushState(\"object or string\", \"" + pageTitle + "\", \"" + pageUrl + "\")");
Related