Primefaces 文件下载错误处理

yod*_*uhe 5 primefaces jsf-2

如何处理primefaces文件下载的错误

<p:fileDownload value="#{testBean.file}" /> 
Run Code Online (Sandbox Code Playgroud)

测试Bean.java

    public StreamedContent getFile() {  
    if(selectedReport ==null){
        FacesContext.getCurrentInstance().addMessage(.."Please select a file");
        return null;//What to do here
    }
    InputStream inps =  ReportGenerator.getPDF(selectedReport);
    return new DefaultStreamedContent(inps, "application/pdf", "report.pdf"); 
    }
Run Code Online (Sandbox Code Playgroud)

yod*_*uhe 3

这有帮助http://forum.primefaces.org/viewtopic.php?f=3&t=8263

<p:commandButton    ajax="false"
                    value="Download Detailed Report"
                    actionListener="#{reportBean.generateDetailedReport}">

    <p:fileDownload value="#{reportBean.detailedReport}"/>

</p:commandButton>
Run Code Online (Sandbox Code Playgroud)
public void generateDetailedReport(ActionEvent ae) {

    ......

    reportStream = ExcelReport.generate(reportList);

    if (reportStream == null) {

        FacesUtil.addError("Error generating report");

        throw new AbortProcessingException();
    }
}

public StreamedContent getDetailedReport() {

    return new DefaultStreamedContent(reportStream, "application/xls", "report.xls"); 
}
Run Code Online (Sandbox Code Playgroud)