1

Im currently looking for a solution to invoke a pagenavigation from "foo.xhtml" to "bar.xhtml" and starting a download dialogue at the same time. I already implemented a solution which worked on my test tomcat, but the JavaScript got cut out on the targetplatform, a WAS 8.0.0.9.

<c:if test="#{Bean.downloadPreconditions()}"> <!-- checks a simple boolean variable which gets set to 'true' after all informations for the download are set-->
            <script>
                window.onload = function() {
                document.getElementById('form:download').onclick();
                }
            </script>

            <h:commandLink  id="download"
                            action="PrimeFaces.monitorDownload(start, stop);">
                            <p:fileDownload value="#{Bean.downloadFile}"></p:fileDownload> 
            </h:commandLink>   

In this solution i start the download via JavaScript AFTER i redirected to the targetpage "bar.xhtml" from "foo.xhtml".

The PreRenderView solution does not work, because i visited the view before and it does not get freshly instantiated.

I tried several slightly different PrimeFaces.monitorDownload(start, stop); version with an attached <p:fileDownload>, as well as a download invoked by a backingbean like it is described here.

I am aware that im trying to call 2 requests to the server with one single click. I want to know if it is still somehow possible to maybe first switch to the target view and after that call the download dialogue in an automated way.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

1 Answers1

1

So, i still could not figure out why my JavaScript did not get executed properly, but i found a workaround:

Instead of calling the commandLink via proper JS at runtime, (Also note here: the commandLink needs to be defined above the JS, or as in my example a commandLink with the functionality already exists somewhere else on this site) I just invoked the link with the hardcoded primefaces syntax:

        <c:if test="#{Bean.downloadPreconditions()}"> <!-- checks a simple boolean variable which gets set to 'true' after all informations for the download are set-->                        
                    <script>
                        window.onload = function() {
                             var firstDownlink = document.getElementById("form:DownloadLink"); //this refers to a link, that already exists on my page, alternativly just create one but do not apply any visual effects to it so it stays hidden
                             firstDownlink.onclick.apply(firstDownlink);
                        }
                    </script>
        </c:if>