Wednesday, August 11, 2010

Wicket Ajax Session time out problem

Wicket is super cool Web framework. Doing Ajax stuff is even more cooler. I just love saying cool , kool , kewl.

Most/All of the applications using wicket need a way to handle session time out with Ajax request.So I thought of putting it on my blog.



Wicket life cycle method made everything very easy and that's just kool(Cant help my self)

In just 2 simple steps you can handle Ajax session time out problem.

So here what you need to do:

Step 1:
First override newRequestCycle method in your webApplication class. Like below



public RequestCycle newRequestCycle(Request request, Response response) {

// TODO Auto-generated method stub

return new CustomRequestCycle(this, (WebRequest)request, response);

}


Step 2:

Create new CustomRequestCycle class which extends WebRequestCycle like below


public class CustomRequestCycle extends WebRequestCycle{



public CustomRequestCycle(final WebApplication application, final WebRequest request, final Response response) {

super(application, request, response);

}

@Override

public final Page onRuntimeException(final Page cause, final RuntimeException e) {

// obviously you can check the instance of the exception and return the appropriate page if desired

if (e instanceof PageExpiredException) {

if ( this.getWebRequest().isAjax()) {

return new YourSignonPage(new PageParameters());

}

}

return new YourCustomErrorPage(e);

}

}


See how I override onRuntimeException method. Just check for PageExpiredException and if its Ajax request , return your Sign On Page.

That's it. Kool :)