Spring MVC 3.0 Jasper-Reports 4在浏览器中引导HTML报告 [英] Spring MVC 3.0 Jasper-Reports 4 Directing HTML reports in browser
问题描述
我正在使用Spring MVC 3和JasperReports。我创建了一些很棒的PDF和Xls报告没有问题。我想要做的是在屏幕上为用户显示创建的HTML报告,作为他们获得的报告的预览,包含在网站模板中。有没有办法做到这一点?
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?
我没有找到关于这个主题的任何教程/文章,我确实找到了一本关于Java开发人员的JasperReports 3.5的书解决了这个(我这是一个菜鸟,所以请耐心等待。)我对此的理解是我必须将输入流重定向到浏览器。我认为必须有一个更简单的方法!以及从中删除HTML报表页眉和页脚的方法。
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";
}
这篇关于Spring MVC 3.0 Jasper-Reports 4在浏览器中引导HTML报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!