开发者

Spring MVC 3.0 Jasper-Reports 4 Directing HTML reports in browser

开发者 https://www.devze.com 2023-03-13 02:39 出处:网络
I am working with Spring MVC 3 and JasperReports. I\'ve created some great PDF and Xls reports without a problem. What I would like to do is display the created HTML report on screen for the user as a

I am working with Spring MVC 3 and JasperReports. I've created some great PDF and Xls reports without a problem. What I would like to do is display the created HTML report on screen for the user as a preview of the report they are getting, wrapped in the website template. Is there a way to do this?

I haven't found any tutorials/articles on this subject, I did find a book on JasperReports 3.5 for Java Developers that kind a addressed this. (I'm a noob on this so bear with me.) My understanding of this is that I have to redirect the input stream to the browser. I figure that there must be an easier way! And a way to strip the开发者_高级运维 HTML report header and footer from it.

Any help would be appreciated!


Instead of using another framework 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";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号