我正在尝试使用JasperReport在单击按钮时生成一个简单的PDF报告。以下是相关代码:
报告.xhtml:
<p:commandButton value="#{msg['report.generate']}" action="#{generateReportBean.generateReport}" />
生成报告豆.java:
public void generateReport() throws JRException, IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext
.getResponse();
InputStream reportStream = facesContext.getExternalContext().getResourceAsStream("/WEB-INF/reports/report.jrxml");
JasperDesign jasperDesign = JRXmlLoader.load(reportStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
byte[] report = JasperRunManager.runReportToPdf(jasperReport, new HashMap(), new JREmptyDataSource());
response.setContentType("application/pdf");
response.setContentLength(report.length);
response.setHeader("Content-disposition", "attachment; filename=report.pdf" );
ServletOutputStream servletOutputStream = response.getOutputStream();
servletOutputStream.write(report);
servletOutputStream.flush();
servletOutputStream.close();
facesContext.responseComplete();
}
问题是当我使用FireBug时,我可以看到一个响应(对我来说没关系):
X-Powered-By JSF/2.0
Content-Type application/pdf
Content-Length 1310
Content-Disposition attachment; filename=report.pdf
Server Jetty(7.4.0.v20110414)
但是我没有收到允许我保存 pdf 的弹出窗口。如您所见,我正在开发 Jetty 7.4 maven 插件。我在这里做错了什么吗?
将 ajax="false" 添加到你的命令按钮
<p:commandButton ajax="false" value="#{msg['report.generate']}" action="#{generateReportBean.generateReport}" />