首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring MVC 3.0 Jasper-Reports 4在浏览器中定向HTML报告

Spring MVC 3.0 Jasper-Reports 4在浏览器中定向HTML报告
EN

Stack Overflow用户
提问于 2011-06-16 06:12:16
回答 1查看 6K关注 0票数 3

我正在使用Spring MVC3和JasperReports。我已经创建了一些很好的PDF和Xls报告,没有任何问题。我想做的是在屏幕上为用户显示创建的HTML报告作为他们得到的报告预览,包装在网站模板中。有没有办法做到这一点?

我没有找到任何关于这个主题的教程/文章,但我找到了一本面向JasperReports开发人员的关于Java3.5的书,这本书很好地解决了这个问题。(在这方面我还是个新手,所以请耐心听我说)我对此的理解是,我必须将输入流重定向到浏览器。我想一定有一种更简单的方法!以及一种从其中剥离HTML报告页眉和页脚的方法。

任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-17 22:16:52

而不是使用另一个框架来解决我的问题。我是这样解决的:

代码语言:javascript
复制
@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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6365045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档