Primefaces 文件下载错误处理

发布于 2024-10-30 17:57:34 字数 509 浏览 0 评论 0原文

如何处理 primefaces filedownload

<p:fileDownload value="#{testBean.file}" /> 

TestBean.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"); 
    }

How to handle error with primefaces filedownload

<p:fileDownload value="#{testBean.file}" /> 

TestBean.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"); 
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吝吻 2024-11-06 17:57:35

这有助于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>
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"); 
}

This helped 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>
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"); 
}
心舞飞扬 2024-11-06 17:57:35

如果您的意思是通过向用户显示 html 消息处理错误,那么您应该将可以抛出异常的代码放在 try...catch 块,catch 块将内容类型更改回 text/html 并以您想要的方式显示错误。

但由于内容类型是添加在 DefaultStreamedContent 中的,也许只有重定向才能解决这个问题。

If you mean handle error by show an html message to the user, then you should put the code that can throw an exception inside a try...catch block, and the catch block changes the content type back to text/html and display the error in the way you want.

But since the content type is added in the DefaultStreamedContent, maybe only a redirection can solve this.

独孤求败 2024-11-06 17:57:35

更好地使用像获取所有异常一样;

} catch (Exception e) {
        handleError(e);
    }

处理错误方法;

private void handleError (Throwable t) {

    try {
        throw t;
    } catch (AccessException ae) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (SQLException sqle) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (ConnectException ce) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (RemoteException re) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (NotBoundException nbe) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (IOException ioe) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (IllegalArgumentException iae) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (Throwable tt) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    }

    FacesUtil.completeResponse();
}

Better to use obtain all exception in one like ;

} catch (Exception e) {
        handleError(e);
    }

handleError method ;

private void handleError (Throwable t) {

    try {
        throw t;
    } catch (AccessException ae) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (SQLException sqle) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (ConnectException ce) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (RemoteException re) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (NotBoundException nbe) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (IOException ioe) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (IllegalArgumentException iae) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    } catch (Throwable tt) {
        FacesUtil.setErrorMessage("CCCCCCC");           
    }

    FacesUtil.completeResponse();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文