primefaces中<p:fileDownload>控件的使用

本文介绍了如何在Web开发中利用PrimeFaces的<p:fileDownload>控件实现报表的下载功能。通过在xhtml页面设置命令按钮和文件下载组件,并在后台 ManagedBean 中提供 StreamedContent 对象,当用户点击按钮时,会触发PDF报表的下载。在Firefox和Chromium浏览器中,该控件表现不同,Firefox允许打开浏览,而Chromium直接下载保存。

1.<p:fileDownload>控件使用背景:

     在Web开发中, 为了实现报表的下载和浏览打印,选择使用PrimeFaces中的<p:fileDownload>控件来实现.

2.完成功能:

  在WEB页面点击按钮时,弹出"打开/保存"的对话框,实现报表文件(pdf格式)的下载和浏览

3.具体使用:

(1)xhtml部分:

<h:form>

              <p:commandButton value="Download" ajax="false" onclick="PrimeFaces.monitorDownload(start, stop);" icon="ui-icon-arrowthichk-s">
                    <p:fileDownload value="#{user.downloadReportfile}" />
                </p:commandButton>          

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

                    function stop() {
                        PF('statusDialog').hide();
                    }
            </script>
 </h:form>

(2)后台ManagedBean:

名称:User

public class User {

    private StreamedContent downloadReportfile;

          public StreamedContent getDownloadReportfile() {
                        InputStream stream = ((ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("/resources /test.pdf");
                       downloadReportfile = new DefaultStreamedContent(stream, "application/pdf", "downloadTest.pdf");
                       return downloadReportfile;
           }

         public void setDownloadReportfile(StreamedContent downloadReportfile) {
         this.downloadReportfile = downloadReportfile;
    }

}

(3)运行效果图:

    (a)Firefox浏览器下,可以打开浏览,也可以保存下载:


(b)Chrominum浏览器下,直接下载保存:





 

       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值