有条件地提供文件下载或显示导出验证错误消息 [英] Conditionally provide either file download or show export validation error message

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

问题描述

我一直在使用CRUD应用程序,我需要将数据从数据库导出到csv文件。
为了导出,我不得不禁用ajax,方式如下:

 < p :commandButton value =Exportajax =falseaction =myController.export/> 

在调用的方法中,我创建文件并通过OmniFaces实用程序方式下载:

  Faces.sendFile(file,true); 

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

  RequestContext.getCurrentInstance()。showMessageInDialog(new FacesMessage(FacesMessage.SEVERITY_WARN,Warning! ,没有可用的出口数据。)); 

现在,尽管所有这些都按预期工作,但问题是因为ajax被禁用,对话框无法动态显示,页面重新加载。如果启用ajax,对话框将动态显示,但文件下载不启动。



我一直在努力解决这个问题,使用监视器下载,或通过强制点击第二个按钮,但到目前为止,我没有取得任何进展。



有没有一般可接受的方法来解决这样的问题?

解决方案


有没有一般可接受的方法来解决这样的问题?你基本上是要先触发一个ajax请求,如果它成功启动了一个完整的检查,然后触发一个同步请求下载文件(注意你不能用ajax下载文件)。您可以使用 FacesContext#validationFailed() (或OmniFaces Faces.validationFailed() )标记通过 args 对象,PrimeFaces在 oncomplete 函数上下文中注入(注意,ajax相关属性,如 oncomplete 在ajax被禁用时不起作用。



这样一个例子:

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





  public void export ){
//在本地准备文件

if(fail){
//显示消息,然后设置验证失败如下。
Faces.validationFailed();
}
}

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

//之后删除本地文件?
}

请注意,隐藏的< p:commandButton>正在使用而不是< p:remoteCommand> ,因为后者不支持 ajax =false / code>。


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 OmniFaces utility method:

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 (note that you can't download files with ajax). 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 (note that ajax related attributes such as oncomplete don't work when ajax is disabled).

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

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

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

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