在非ajax调用中在客户端执行JavaScript [英] Execute JavaScript on client side within an non ajax call

查看:79
本文介绍了在非ajax调用中在客户端执行JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,用户可以按下按钮创建和下载文件。该过程需要一些时间,所以我想在此过程中阻止UI,直到下载窗口出现。

In my application the user is able to push a button to create and download a file. The process takes some time, so I want to block the UI during this process until the download window appears.

操作方法是我处理响应看起来基本上像这样:

The action method were I am handling the response looks basically like this:

public void download() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    // set content disposition etc.    
    XSSFWorkbook wb = getWorkbook();
    wb.write(externalContext.getResponseOutputStream());            
    facesContext.responseComplete();
}



在我的JSF视图中,我触发一个blockUI组件来禁用按钮:

In my JSF view I am triggering a blockUI component to disable the button like this:

<p:commandButton value="Doanload" id="b" 
                 action="#{bean.doanload()}"
                 ajax="false" 
                 onclick="PF('crBui').show();"  />
<p:blockUI block=":trGrid" widgetVar="crBui" trigger="b">
     <p:graphicImage value="/images/loading.gif" alt="loading..."/>
</p:blockUI>

我试图使用PrimeFaces RequestContext来执行一些JavaScript来隐藏blockUI组件,但这不是工作。不执行JavaScript:

I tried to use the PrimeFaces RequestContext to execute some JavaScript to hide the blockUI component, but that does not work. The JavaScript is not executed:

    public void download() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        // set content disposition etc.    
        XSSFWorkbook wb = getWorkbook();
        wb.write(externalContext.getResponseOutputStream());    
        RequestContext.getCurrentInstance()
                      .execute("PF('crBui').hide();");        
        facesContext.responseComplete();
    }

如果我使用ajax调用而不是非ajax调用,

If I am using a ajax call instead of the non-ajax call, then the file download does not work any longer.

任何建议如何改善我的功能?

Any suggestions how could I archieve my functionality?

推荐答案

我终于使用PrimeFaces的PrimeFaces.monitorDownload()'

I finally ended up using PrimeFaces 'PrimeFaces.monitorDownload()'

在我看来:

<p:commandButton value="Doanload" id="b" 
             action="#{bean.doanload()}"
             ajax="false" 
             onclick="PrimeFaces.monitorDownload(start, stop);"  />

<script type="text/javascript">
    function start() {
        PF('crBui').show();
    }
    function stop() {
        PF('crBui').hide();
    }
</script>

获取DownloadMonitor工作的主要技巧是在响应中设置一个Cookie:

The main trick to get the DownloadMonitor working is to simply set a Cookie in the response:

externalContext.addResponseCookie(
    org.primefaces.util.Constants.DOWNLOAD_COOKIE,
    "true",
    Collections.<String, Object>emptyMap()
);

这样UI元素被阻塞,直到FileDownload的窗口出现,这正是我的想要在最后实现。

That way the UI-elements are blocked until the window for the FileDownload appears, which is exactly what I wanted to achieve in the very end.

这篇关于在非ajax调用中在客户端执行JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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