在JSF中不使用JS调用导航和文件下载

在JSF中不使用JS调用导航和文件下载,jsf,primefaces,download,navigation,Jsf,Primefaces,Download,Navigation,我目前正在寻找一个解决方案来调用从“foo.xhtml”到“bar.xhtml”的页面导航,同时启动下载对话。我已经在我的测试tomcat上实现了一个解决方案,但是JavaScript在targetplatform上被删除了,a是8.0.0.9 <c:if test="#{Bean.downloadPreconditions()}"> <!-- checks a simple boolean variable which gets set to 'true' after all

我目前正在寻找一个解决方案来调用从“foo.xhtml”到“bar.xhtml”的页面导航,同时启动下载对话。我已经在我的测试tomcat上实现了一个解决方案,但是JavaScript在targetplatform上被删除了,a是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>   

window.onload=函数(){
document.getElementById('form:download').onclick();
}
在这个解决方案中,我从“foo.xhtml”重定向到targetpage“bar.xhtml”后,通过JavaScript开始下载

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

我尝试了几个稍微不同的
PrimeFaces.monitorDownload(开始,停止)版本,附带一个
,以及一个由backingbean调用的下载,如前所述


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

因此,我仍然无法理解为什么我的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>

window.onload=函数(){
var firstDownlink=document.getElementById(“form:DownloadLink”);//这指的是一个链接,它已经存在于我的页面上,或者只创建一个链接,但不对其应用任何视觉效果,因此它保持隐藏状态
firstDownlink.onclick.apply(firstDownlink);
}