1

Is there any way to do that?

I heard something about implemented RequestCycle, how to acomplish that?

Tried How can I get the responsePage from a RequestCycle in Wicket 1.5? this, but doesnt work.

Community
  • 1
  • 1
SSV
  • 860
  • 3
  • 11
  • 25

2 Answers2

2

The reason why you get a PageExpiredException in Wicket is because Wicket is unable to find the page. There is no way of determining the type of the page that is no longer available, because, well, the page actually is no longer there. It ceased to exist, met its maker, bereft of life, rests in peace, its lifecycle are now 'istory, kicked the bucket. It is an ex-page.

So Wicket's only recourse is to serve the PageExpiredException, and there is no way (in Wicket itself) to retrieve the page that was attempted to resurrect.

Now what you can try to do is to store the class of the last rendered page in the session, and use that in your RequestCycleListener implementation of onException() and return the appropriate request handler. In code:

@Override
public void onRequestHandlerExecuted(RequestCycle cycle, IRequestHandler handler) {
    Class<? extends Page> p = null;

    if(handler instanceof IPageRequestHandler)
        p = ((IPageRequestHandler)handler).getPageClass();
    else if(handler instanceof IComponentRequestHandler)
        p = ((IComponentRequestHandler)handler).getComponent().getPage().getClass();

    MySession.get().setLastPageClass(p);
}

@Override
public IRequestHandler onException(RequestCycle cycle, Exception ex) {
    Class<? extends Page> pageClass MySession.get().getLastPageClass();
    ... return some handler based on your logic
}

You might want to check for more IRequestHandler implementations in onRequestHandlerExecuted.

Martijn Dashorst
  • 3,652
  • 17
  • 26
  • Hi, I think You misunderstood my question :) What I want is to simply redirect user while pageexpired occurs to another page so he doesnt see "Page Expired" on his page. I want it only on one specific page, so getApplicationSettings().setPageExpiredErrorPage(PAGE.class) is not a good choice :( – SSV Aug 06 '13 at 07:38
  • And my answer works around the general purpose setPageExpiredErrorPage... try it – Martijn Dashorst Aug 07 '13 at 08:34
1

If I understand correctly you want to redirect user only if pageExpired happened from specific page? You can try something like this in you implementation of Application:

getRequestCycleListeners().add(new AbstractRequestCycleListener() {
   @Override
   public IRequestHandler onException(RequestCycle cycle, Exception e) {
      if(e.getClass().equals(PageExpiredException.class)) {
         //check if url in request responds to correct mounted page type
         if(isPageUrl(cycle.getRequest().getUrl()))) {
            return new RenderPageRequestHandler(new PageProvider(MyPage.class));
         } else {
            return super.onException(cycle, e);
         }
      } else {
         return super.onException(cycle, e);
      }
   }
}

This assumes few things - that the page at which you got the exception has been mounted, and that you will be able to parse request url to be sure it is it. I haven't tested it but we are doing something similar.

Michał Krzemiński
  • 1,261
  • 8
  • 10