我正在使用PrimeFaces 3.5、JasperReports 5.2和JSF 2.x开发应用程序。我的旧应用程序中有一些由JasperReports 4.5和JSF 1.2准备的报告页面。所有这些报告页面都在旧应用程序中工作。我试图在我的新应用程序中传输这些报告页面。在新应用程序中一切正常,但报告无法导出,我没有收到任何错误。我错过了什么吗?
这是我的代码,它在我的旧Jsf 1.2应用程序中完美运行,但不适用于新应用程序。
private void prepareReport(String reportPath, @SuppressWarnings("rawtypes") List beanList, String outputFormat, String reportName) {
JasperReport jReport = null;
JasperPrint jPrint = null;
try {
jReport = JasperCompileManager.compileReport(reportPath);
jPrint = JasperFillManager.fillReport(jReport, jasperParameter, new JRBeanCollectionDataSource(beanList));
} catch (JRException e) {
e.printStackTrace();
}
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (outputFormat.equals(OUTPUT_FORMAT_PDF)) {
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment; filename=" + reportName + ".pdf");
JasperExportManager.exportReportToPdfStream(jPrint, outputStream);
} else if (outputFormat.equals(OUTPUT_FORMAT_EXCEL)) {
response.setContentType("application/xls");
response.setHeader("Content-disposition", "attachment; filename=" + reportName + ".xls");
JExcelApiExporter exporter = new JExcelApiExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
exporter.exportReport();
} else if (outputFormat.equals(OUTPUT_FORMAT_RTF)) {
response.setContentType("application/rtf");
response.setHeader("Content-disposition", "attachment; filename=" + reportName + ".rtf");
JRRtfExporter exporter = new JRRtfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
exporter.exportReport();
}
} catch (JRException e) {
e.printStackTrace();
}
try {
outputStream.flush();
outputStream.close();
FacesContext.getCurrentInstance().responseComplete();
} catch (IOException e) {
e.printStackTrace();
}
}
这是我称之为支持 bean 的 JSF 2 xhtml 页面的一部分:
<h:panelGrid columns="4" columnClasses="alignTop,alignTop,alignTop,alignTop">
<p:commandButton id="pdfFormatBtn" actionListener="#{reports.pdfFormatSelected}" value="PDF" immediate="true"
icon="pdfIcon" styleClass="tableCell"/>
<p:commandButton id="excelFormatBtn" actionListener="#{reports.pdfFormatSelected}" value="EXCEL" immediate="true"
icon="excelIcon" styleClass="tableCell"/>
<p:commandButton id="rtfFormatBtn" actionListener="#{reports.pdfFormatSelected}" value="RTF" immediate="true"
icon="rtfIcon" styleClass="tableCell"/>
<p:commandButton id="cleanBtn" actionListener="#" value="#{general.clean}" immediate="true"
icon="ui-icon-refresh" styleClass="tableCell"/>
</h:panelGrid>