'Primefaces 5.1 start ajaxStatus during preRenderView

In primefaces is possible start "ajax status" from a method of a backing beans?

I have the following view:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://java.sun.com/jsf/core"
                template="/pages/templates/template.xhtml">
    <ui:define name="metadata">
    <f:event type="preRenderView" listener="#{preloadBean.init}" />
    </ui:define>    
    <ui:define name="content"> 
    <h:form id="form" >
    <p:growl id="growl" />
    <p:commandButton id="btn" value="Trigger ajaxStatus" actionListener="#{preloadBean.buttonAction}" /> 
    <f:facet name="start">
        <p:graphicImage name="images/ajaxloadingbar.gif" />
    </f:facet> 
    <f:facet name="complete">
         <h:panelGrid columns="4">
            <p:outputLabel value="#{preloadBean.nomeCognomeVis} - #{preloadBean.poloVis}" style="font-weight:bold" />
            <h:outputText value="Aggiornamento delle: #{preloadBean.ultimoAggiornamento}"></h:outputText>
            <p:commandButton actionListener="#{preloadBean.buttonAction}" id="aggiornaDati" update="growl" value="Aggiorna Dati" title="Aggiorna Dati" styleClass="ui-priority-primary" />
            <p:commandButton actionListener="#{preloadBean.buttonAction}" id="impersonifica" update="growl" value="Ut.P" title="Inpresonifica utente" styleClass="ui-priority-primary" />
        </h:panelGrid>  
    <p:dataTable var="record" value="#{preloadBean.records}" paginator="false" id="mainTable">        
        <p:column headerText="Polo" sortBy="#{record.polo}" filterBy="#{record.polo}" filterStyle="display:none">
            <h:outputText value="#{record.polo}" />
        </p:column>
        <p:column headerText="Operatore" sortBy="#{record.operatore}" filterBy="#{record.operatore}" filterStyle="display:none">
            <h:outputText value="#{record.operatore}" />
        </p:column>
        <p:column headerText="Aperti" sortBy="#{record.totaleAperti}" filterBy="#{record.totaleAperti}" filterStyle="display:none">
            <h:outputText value="#{record.totaleAperti}" />
        </p:column>      
    </p:dataTable>
    </f:facet>
</p:ajaxStatus>
    <p:ajaxStatus onstart="PF('statusDialog').show()" onsuccess="PF('statusDialog').hide()" />  
    <p:dialog widgetVar="statusDialog" modal="true" draggable="false" closable="false" resizable="false" showHeader="false">
    <p:graphicImage name="images/ajaxloadingbar.gif" />
</p:dialog>   
    </h:form>      
    </ui:define>
</ui:composition>

This work only when I click on "btn" commandButton.

I want to load the ajax status when the page get a refresh, or in the first access.



Solution 1:[1]

Here is an example to put in the footer of your HTML:

<p:ajaxStatus onstart="PF('statusDialog').show();" 
              oncomplete="PF('statusDialog').hide();"
              onerror="alert('Error msg. Please try again!');" />

<p:dialog widgetVar="statusDialog" modal="true">
    <h:form prependId="false">
        <p:outputLabel value="Processing" style="display: block; margin: 10px;" />
        <p:graphicImage library="images/icones" name="glow-ring.gif" />
        <p:outputLabel value="Wait!" style="display: block; margin: 10px;" />
    </h:form>
</p:dialog>

The dialog only starts when you call an ajax request.

If you want only show the dialog on start, whitout any ajax request you can use this:

<p:remoteCommand name="onload" autoRun="true" process="@this" immediate="true"
                 onstart="PF('statusDialog').show();" 
                 oncomplete="PF('statusDialog').hide();" />

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1