有条件地提供任一文件下载或显示消息对话框 [英] Conditionally provide either file download or show message dialog

查看:142
本文介绍了有条件地提供任一文件下载或显示消息对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在工作的CRUD应用程序,我需要将数据从数据库导出到csv文件。 为了出口,我只好取消阿贾克斯,在以下code所示的方式:

 <电话号码:的commandButton值=导出阿贾克斯=假行动=myController.export/>
 

在调用的方法,我创建文件,并通过下载:

  Faces.sendFile(文件,真正的);
 

用同样的方法,我检查是否有实际的任何数据,而如果没有任何数据,将显示警告对话框:

  RequestContext.getCurrentInstance()showMessageInDialog(新的FacesMessage(FacesMessage.SEVERITY_WARN,警告!,没有可用的数据用于出口。));
 

现在,而所有这一切都不会工作打算,这个问题是,由于AJAX被禁用,对话不能动态示出,并加载页面。如果AJAX启用,动态显示对话框,但文件没有开始下载。

我一直在试图解决这个问题,通过监控下载< /一>,或以武力点击第二个按钮,但到目前为止,我还没有做出任何关于该事项的进展。

有没有解决问题,像这样的任何一般可接受的方式?

解决方案
  

有没有解决的问题像这样的任何一般可接受的方式?

您基本上要第一,并在其的onComplete检查触发一个Ajax请求,如果它的成功,然后触发同步请求下载文件。你可以使用 <$ C $的C>的FacesContext#validationFailed() (或OmniFaces的 Faces.validationFailed() )来标记验证失败状态,通过可的args 对象,PrimeFaces注入了的onComplete 功能方面。

事情是这样的:

 &LT;电话号码:的commandButton
    值=出口
    行动=#{bean.export}
    的onComplete =如果(参数及放大器;放大器;&放大器;放大器;!args.validationFailed)。PF(下载)jq.click()/&GT;
&LT;电话号码:的commandButton
    widgetVar =下载
    的styleClass =UI辅助隐藏
    行动=#{bean.download}阿贾克斯=FALSE/&GT;
 

 公共无效的出口(){
    // prepare文件。

    如果(故障){
        //显示信息并设置验证失败如下。
        Faces.validationFailed();
    }
}

公共无效下载()抛出IOException异常{
    Faces.sendFile(文件,真正的);
}
 

请注意,一个隐藏的&LT;电话号码:的commandButton&GT; 正在使用,而不是&LT;电话号码:remoteCommand&GT; 因为后者不支持阿贾克斯=假

I've been working on CRUD application, and I need to export data from database to csv file. In order to export, I had to disable ajax, in a manner shown in following code:

<p:commandButton value="Export" ajax="false" action="myController.export"/>

In the method invoked, I create the file and download it via:

Faces.sendFile(file, true);

Using the same method, I check if there actually is any data, and if there isn't any data, warning dialog is shown:

RequestContext.getCurrentInstance().showMessageInDialog(new FacesMessage(FacesMessage.SEVERITY_WARN, "Warning!", "No available data for export."));

Now, while all of this does work as intended, the problem is that because ajax is disabled, dialog cannot be dynamically shown, and page is reloaded. If ajax is enabled, dialog is shown dynamically, but file download doesn't start.

I've been trying to work around this issue, by using Monitor download, or by force clicking second button, but so far I haven't made any progress on the matter.

Is there any generally acceptable way of solving issues like this one?

解决方案

Is there any generally acceptable way of solving issues like this one?

You basically want to fire an ajax request first and in its oncomplete check if it's successful, and then trigger a synchronous request to download the file. You could make use of FacesContext#validationFailed() (or OmniFaces Faces.validationFailed()) to mark the validation fail state which is available via args object which PrimeFaces injects in the oncomplete function context.

Something like this:

<p:commandButton 
    value="Export" 
    action="#{bean.export}" 
    oncomplete="if (args &amp;&amp; !args.validationFailed) PF('download').jq.click()" />
<p:commandButton 
    widgetVar="download" 
    styleClass="ui-helper-hidden" 
    action="#{bean.download}" ajax="false" />

public void export() {
    // Prepare file.

    if (fail) {
        // Show message and set validation failed as below.
        Faces.validationFailed();
    }
}

public void download() throws IOException {
    Faces.sendFile(file, true);
}

Note that a hidden <p:commandButton> is being used instead of <p:remoteCommand> as the latter doesn't support ajax="false".

这篇关于有条件地提供任一文件下载或显示消息对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆