Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show loading progress when making JSF Ajax request

How can I show some loading message when making request using <f:ajax>?

like image 949
anfy2002us Avatar asked Oct 24 '11 19:10

anfy2002us


People also ask

How to show progress bar while loading using ajax?

The first function calls an action via ajax on my controller and passes two parameters. Use the ajaxStart to start your progress bar code. $(document). ajaxStop(function(){ // hide the progress bar $("#progressDialog").

How do I know if ajax request is running?

You could use ajaxStart and ajaxStop to keep track of when requests are active. Show activity on this post. Show activity on this post. Checking for null to determine if the request object exists is important, but if it is not null you should really check request.

What is the tag for ajax support in JSF?

JavaServer Faces (JSF) JSF provides execellent support for making ajax call. It provides f:ajax tag to handle ajax calls.

What is ajax in Primefaces?

Ajax Attributes It is used to process in partial request. Immediate. false. boolean. It returns a boolean value that determines the phaseId, when true actions are processed at apply_request_values, when false at invoke_application phase.


2 Answers

If you're not already using a 3rd party component library which could already have a ready-made component for that, such as PrimeFaces with <p:ajaxStatus>, then you can use the JSF-provided JavaScript jsf.ajax.addOnEvent() function (and eventually also jsf.ajax.addOnError()) to hook a function on ajax events.

Here's a basic kickoff example:

<script>
    jsf.ajax.addOnEvent(function(data) {
        var ajaxstatus = data.status; // Can be "begin", "complete" and "success"
        var ajaxloader = document.getElementById("ajaxloader");

        switch (ajaxstatus) {
            case "begin": // This is called right before ajax request is been sent.
                ajaxloader.style.display = 'block';
                break;

            case "complete": // This is called right after ajax response is received.
                ajaxloader.style.display = 'none';
                break;

            case "success": // This is called when ajax response is successfully processed.
                // NOOP.
                break;
        }
    });
</script>

<img id="ajaxloader" src="ajaxloader.gif" style="display: none;" />

See also chapter 13.3.5.2 of the JSF 2.0 specification:

13.3.5.2 Monitoring Events For All Ajax Requests

The JavaScript API provides the jsf.ajax.addOnEvent function that can be used to register a JavaScript function that will be notified when any Ajax request/response event occurs. Refer to Section 14.4 “Registering Callback Functions” for more details. The jsf.ajax.addOnEvent function accepts a JavaScript function argument that will be notified when events occur during any Ajax request/response event cycle. The implementation must ensure the JavaScript function that is registered must be called in accordance with the events outlined in Section TABLE 14-3 “Events”.

You can grab some cool ajax loader gifs for free from http://www.ajaxload.info, by the way.

like image 82
BalusC Avatar answered Oct 13 '22 00:10

BalusC


richfaces has a very easy to use component that I use like this:

<a4j:status startText="" stopText="" onstart="showAjaxActive();" onstop="hideAjaxActive();"/>
like image 33
Milo van der Zee Avatar answered Oct 13 '22 01:10

Milo van der Zee