在没有JS的情况下在JSF中调用Navigation和FileDownload

时间:2017-10-13 11:17:46

标签: jsf primefaces download navigation

我目前正在寻找一种解决方案来调用从“foo.xhtml”到“bar.xhtml”的pagenavigation并同时开始下载对话。我已经实现了一个解决方案,该解决方案适用于我的测试tomcat,但是在目标平台上删除了JavaScript,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>   

在此解决方案中,我通过JavaScript开始下载,然后从“foo.xhtml”重定向到目标页面“bar.xhtml”。

PreRenderView解决方案不起作用,因为我之前访问过该视图并且没有得到新的实例化。

我尝试了几个略有不同的PrimeFaces.monitorDownload(start, stop);版本,其附加的<p:fileDownload>,以及由backbean调用的下载,如here所述。

我知道我只需点击一下即可尝试向服务器调用2个请求。我想知道是否仍然可能首先切换到目标视图,然后以自动方式调用下载对话框。

1 个答案:

答案 0 :(得分:1)

所以,我仍然无法弄清楚为什么我的JavaScript没有正确执行,但我找到了解决方法:

而不是在运行时通过适当的JS调用commandLink(另请注意:commandLink需要在JS上面定义,或者在我的示例中,commandLink的功能已存在于此站点的其他位置)我刚刚调用与硬编码的primefaces语法的链接:

        <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>