Spring MVC 3.0 Jasper-Reports 4 HTML Browser Management

I work with Spring MVC 3 and JasperReports. I created some great reports in PDF and Xls without any problems. What I would like to do is display the generated HTML report on the screen for the user as a preview of the report they receive, wrapped in a website template. Is there any way to do this?

I did not find any tutorial / article on this subject, I found the book about JasperReports 3.5 for Java developers that I spoke about. (I'm new to this, so bear with me.) I understand that I need to redirect the input stream to the browser. I suppose there should be an easier way! And a way to strip the HTML header and footer from it.

Any help would be appreciated!

+3
source share
1 answer

Instead of using a different structure to solve my problem. I solved it like this:

@RequestMapping(value = "/report", method = RequestMethod.POST)
public String htmlReport(@RequestParam(value = "beginDate") Date begin,
        @RequestParam(value = "endDate", required = false) Date end,
        ModelMap map) {

    try {

        // Setup my data connection
        OracleDataSource ds = new OracleDataSource();
        ds.setURL("jdbc:oracle:thin:user/password@10.10.10.10:1521:tst3");

        Connection conn = ds.getConnection();

        // Get the jasper report object located in package org.dphhs.tarts.reports
        // Load it 
        InputStream reportStream = this.getClass().getResourceAsStream("reports/tartsCostAllocation.jasper");
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportStream);

        // Populate report with data
        JasperPrint jasperPrint =
            JasperFillManager.fillReport(jasperReport, new HashMap(), conn);

        // Create report exporter to be in Html
        JRExporter exporter = new JRHtmlExporter();

        // Create string buffer to store completed report
        StringBuffer sb = new StringBuffer();

        // Setup report, no header, no footer, no images for layout
        exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "");
        exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "");
        exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);

        // When report is exported send to string buffer
        exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sb);
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

        // Export the report, store to sb
        exporter.exportReport();

        // Use Jsoup to clean the report table html to output to browser
        Whitelist allowedHtml = new Whitelist();
        allowedHtml.addTags("table", "tr", "td", "span");
        allowedHtml.addTags("table", "style", "cellpadding", "cellspacing", "border", "bgcolor");
        allowedHtml.addAttributes("tr", "valign");
        allowedHtml.addAttributes("td", "colspan", "style");
        allowedHtml.addAttributes("span", "style");
        String html = Jsoup.clean(sb.toString(), allowedHtml);

        // Add report to map
        map.addAttribute("report", html);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return "costallocation/report";
}
+4
source

All Articles