简体   繁体   中英

JasperReports can't export data with JSF 2 and PrimeFaces 3.5

I am developing an application with PrimeFaces 3.5 , JasperReports 5.2 , and JSF 2.x . I have some report pages in my old application prepared by JasperReports 4.5 and JSF 1.2 . All those report pages are working in the old application. I tried to transfer those report pages in my new application. Everything works fine in the new app but report cannot be exported and I don't get any error. Am I missing something?

Here is my code that works perfectly in my old Jsf 1.2 app but not with the new app.

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();
    }
}

Here is part of the JSF 2 xhtml page that I call the backing bean:

<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>

尝试使用ajax =“ false”

<p:commandButton id="pdfFormatBtn" actionListener="#{reports.pdfFormatSelected}" value="PDF" immediate="true" icon="pdfIcon" styleClass="tableCell" ajax="false"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM