• Home
  • Contact Me
  • About Me
  • Resources
  • Favorites
  • Earn Money

Thursday, January 8, 2009

JSF: Displaying "Loading....Please Wait!" message during ajax call

AJAX is becoming a specification in present day web application. Web 2 already infected and spread all across internet application. Web 3 is knocking at the doop step. more...

A very common requirement in ajax based web page is to convey the user that some server-side process is going on and the page/portion of the page is reloading. The best way to do this is displaying a suitable message like "Loading.....Please Wait!". Java Server Faces (JSF) is very impressive in implementing AJAX behavior in a page. It has provided well abstracted and easy to implement steps. Now coming back to message displaying use case; it requires just 4 additional changes:
  1. Download a ajax activity image and store in images folder under WebContent folder. You can refer to this site to generate a gif image http://www.ajaxload.info/
  2. Copy the below code anywhere in the page. Preferrably immediately after <body>
    <div id="loading"><p>Loading... Please wait!</p><img src="${pageContext.request.contextPath}/images/activity.gif" /></div>
  3. Add two javascript function as mentioned below:

    <script language="JavaScript">function startProgressBar(){ document.all.loading.style.visibility="visible";}function stopProgressBar(){ document.all.loading.style.visibility="hidden";}</script>
  4. Change the ajax tag in your jsf page
    <hx:ajaxRefreshRequest target="ajaxGroup" onstart="startProgressBar()" oncomplete="stopProgressBar()" id="ajaxRefreshRequest1"></hx:ajaxRefreshRequest>

Regards

Monu