1

I need to show loading image while execute request when execute a f:viewaction, this sound as put f:ajax within f:viewaction, but this is impossible.

<ui:define name="metadata">
    <f:metadata>
        <f:viewAction
            action="#{subscriptionManagementMB.loadSubscriptionManagement()}" />
    </f:metadata>
</ui:define>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • You want to show the loading image on page load? Or when a command component triggers a request\ – kolossus Apr 26 '15 at 17:31
  • Yes. f viewaction execute on page load. But this f viewaction send a request to server (to ManagedBean) – danielolanoburga Apr 26 '15 at 17:34
  • The viewaction runs ON the server initiated FROM the server, not from the client so it does not need to have ajax. So do you want to show something during loading of the page or afterwards? Then please adapt your question – Kukeltje Apr 26 '15 at 21:19
  • @Kukeltje I want show an image during loading the page. – danielolanoburga Apr 26 '15 at 23:34

1 Answers1

1

There's a conceptual thinking mistake here. You can't show an image as long as the page with the image isn't loaded (whatever that image represents is irrelevant to the problem). In order to show an image, you need to make sure that the page with the image is fully loaded first. Only when the page with the image is fully loaded, then you can invoke the desired business action.

The <f:viewAction> is insuitable for this. The common approach is to let JavaScript trigger a server side action when end of <body> is reached, or during document ready/complete event, or during window load event. The exact answer really depends on the libraries you've at hands. The code can be insanely simplified when you've PrimeFaces and/or jQuery at hand.

Let's assume "plain vanilla" JSF/JS, here's a hacky way how to achieve the requirement:

<h:body>
    <h:panelGroup id="content">
        <h:graphicImage name="loading.gif" rendered="#{not bean.loaded}" />

        <ui:fragment rendered="#{bean.loaded}">
            ... (put your content here)
        </ui:fragment>
    </h:panelGroup>

    <h:form id="form" style="display:none;">
        <h:commandButton id="button" action="#{bean.onload}">
            <f:ajax render=":content" />
        </h:commandButton>
    </h:form>

    <h:outputScript target="body">
        document.getElementById("form:button").onclick();
    </h:outputScript>
</h:body>

The image shows the loading image. The form, which is hidden using CSS display:none, holds an ajax command button which updates the content. The script with target="body" will be relocated to end of <body> and invoke the hidden ajax command button when page is loaded.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • In order to show an image from the server after an 'action', the page must be fully loaded. But from what I make from the comments, just a splashscreen is wanted. That is possible. The mentioning of the `` by the OP seems to have been an XY problem – Kukeltje Apr 27 '15 at 08:12
  • @Kukeltje: from what I gathered, OP wants to invoke a long running business action during page load. This answer guarantees that the image is shown before the action is invoked. As to the X of the XY problem, all I can see is a serious performance problem in business/service/DB layer which OP tried to cover this way, but that's beyond the scope of JSF. – BalusC Apr 27 '15 at 08:18
  • Hahaha thanks… you to. Almost celebrated this on Kòrsou. Now we leave in two weeks. – Kukeltje Apr 27 '15 at 08:54
  • Thanks @BalusC. I thought there was any way to use the f viewAction with a call to ajax, but I see that is not possible. Thanks for your code. – danielolanoburga Apr 28 '15 at 14:42