【Primefaces】下载文件时添加loading

下载实现: html可以用不同的标签来实现

	// html 示例1
	<h:commandLink>
	    <p:commandButton value="DownLoad"/>
	    <p:fileDownload value="#{bean.export()}"/>
	</h:commandLink>
	
	// html 示例2
	<p:commandLink value="DownLoad" ajax="false" actionListener="#{bean.export()}"/>
	// html 示例3
	<p:commandButton value="Download" ajax="false">
	    <p:fileDownload value="#{bean.export()}"/>
	</p:commandButton>

添加loading

方案分析

  1. 在标签上添加onstart oncomplete事件;发现这两个方法都是ajax方式调用时才会触发的方法
  2. onclick中触发loadingbean.export()中结束时,执行js方法PrimeFaces.current().executeScript(" $('.loading').modal('hide');");,关闭loading;想象很美好,现实很残酷…
    bean.export()中添加执行js时也不生效,猜测是 解析出的a标签在执行submit时刷新页面时导致js不执行
  3. 终于找到了 PrimeFaces.monitorDownload,可是发现它依然无法关闭loading,主要是stop事件触发不到
    查看 monitorDownload源码 , setInterval 的停止条件是cookie值为true,则需要在下载结束阶段设置cookie
// PrimeFaces.monitorDownload源码
window.PrimeFaces.monitorDownload = function (start, complete, monitorKey) {
    if (this.cookiesEnabled()) {
        if (start) {
            start();
        }
        var cookieName = monitorKey ? 'primefaces.download_' + monitorKey : 'primefaces.download';
        window.downloadMonitor = setInterval(function () {
            var downloadComplete = getCookieByName(cookieName);
            // download完成(complete)时的触发条件
            if (downloadComplete === 'true') {
                if (complete) {
                    complete();
                }
                clearInterval(window.downloadMonitor);
                PrimeFaces.setCookie(cookieName, null);
            }
        }, 1000);
    }
}

最终方案

  1. 页面html代码
<p:commandButton value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start,stop,'downFile')">
    <p:fileDownload value="#{bean.export()}" monitorKey="downFile"/>
</p:commandButton>
<script type="text/javascript">
    function start() {
        $('.loading').modal();
    }
    function stop() {
        $('.loading').modal('hide');
    }
</script>
  1. 当然,下载结束是在bean.export()中触发的,但此处执行html中的stop不生效。则使用java代码添加cookie
    export方法中添加下面代码即可:
/** java 添加cookie,根据PrimeFaces.monitorDownload源码确定:
  * name与primefaces <p:fileDownload> 的 monitorKey 对应,拼接而得
  * value为 字符串'true'
 */
HttpServletResponse response = (HttpServletResponse) facesContext
                .getCurrentInstance().getExternalContext().getResponse();
// 在response时添加cookie
Cookie cookie = new Cookie("primefaces.download_downFile", "true");
response.addCookie(cookie);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值