有没有普遍接受的解决此类问题的方法?
基本上,您首先要触发ajax请求,并在其oncomplete检查中是否成功,然后触发同步请求以下载文件(请注意,您无法使用ajax下载文件)。您可以使用
FacesContext#validationFailed()(或OmniFaces
Faces.validationFailed())标记验证失败状态,该状态可以通过
argsPrimeFaces在
oncomplete函数上下文中注入的对象获得(请注意,与ajax相关的属性(例如,
oncomplete禁用ajax时不起作用))。
像这样:
<p:commandButton value="Export" action="#{bean.export}" oncomplete="if (args && !args.validationFailed) PF('download').jq.click()" /><p:commandButton widgetVar="download" styleClass="ui-helper-hidden" action="#{bean.download}" ajax="false" />public void export() { // Prepare file locally. if (fail) { // Show message your way and then set validation failed as below. Faces.validationFailed(); }}public void download() throws IOException { Faces.sendFile(file, true); // Delete local file afterwards?}请注意,
<p:commandButton>正在使用隐藏而不是
<p:remoteCommand>后者不支持
ajax="false"。



