我正在尝试使用JasperReport在按钮点击时生成一个简单的PDF报告。这是相关的代码:
report.xhtml:
<p:commandButton value="#{msg['report.generate']}" action="#{generateReportBean.generateReport}" />
GenerateReportBean.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插件进行开发。我在这里做错了吗?
答案 0 :(得分:5)
将ajax =“false”添加到commandButton
<p:commandButton ajax="false" value="#{msg['report.generate']}" action="#{generateReportBean.generateReport}" />