Primefaces 文件下载错误处理
如何处理 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这有助于http://forum.primefaces.org/viewtopic.php? f=3&t=8263
This helped http://forum.primefaces.org/viewtopic.php?f=3&t=8263
如果您的意思是通过向用户显示 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.更好地使用像获取所有异常一样;
处理错误方法;
Better to use obtain all exception in one like ;
handleError method ;