在没有JS的情况下在JSF中调用导航和FileDownload
问题描述:
我正在寻找一种解决方案来调用从“foo.xhtml”到“bar.xhtml”的页面导航并同时启动下载对话框。我已经实现了一个可以在我的测试tomcat上运行的解决方案,但是JavaScript已经在目标平台WAS 8.0.0.9上被切断了。在没有JS的情况下在JSF中调用导航和FileDownload
<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开始下载后,我重定向到“f.x.xtml”目标页“bar.xhtml”。
PreRenderView解决方案无法正常工作,因为我之前访问过该视图,并且没有得到新的实例化。
我尝试了几个稍微不同的PrimeFaces.monitorDownload(start, stop);
版本,附带<p:fileDownload>
,以及由backingbean调用的下载,如here所述。
我知道,即时通讯尝试调用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>