我正在使用带有 JasperReports 的 Spring MVC 3.0 对 Web 应用程序进行原型(prototype)设计。
我已经使用 Spring + Jfreechart + iText + Apache POI 完成了报告应用程序,并且能够成功地使用 Spring 提供的相应 View 类来流式传输 pdf、xls 和图像。
这次我想尝试使用 JasperReports,这样我就可以在应用程序之外设计我的 pdf,而不必担心了解底层 api(即 jfreechart、itext 或 poi)。
问题
我有一个包含 的 report1.jrxml 文件查询字符串 用我的查询标记两个日期参数。当我通过 iReport 测试报告时,它编译并运行成功。这里没有问题。
现在我正在阅读以下 Spring 文档 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#view-jasper-reports 中的 JasperReports 部分
我试图让 JasperReportsMultiFormatView 正常工作,但有一件我不明白:
可以做什么
JaperReports 提供了一组负责编译、归档和导出报表的 xxxManager 对象。您可以创建一个实现 Spring View 接口(interface)的自定义类并执行以下操作:
Connection connection;
ServletOutputStream servletOutputStream = response .getOutputStream();
InputStream reportStream = getServlet().getServletConfig().getServletContext().getResourceAsStream("/reports/report1.jasper");
response.setContentType("application/pdf");
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:
3306/flightstats?user=user&password=secret");
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), connection);
connection.close();
servletOutputStream.flush();
servletOutputStream.close();
我需要什么
我需要完成上面那些利用 Spring 类的代码,例如 JasperReportsPdfView、JasperReportsXlsView,甚至更好的 JasperReportsMultiFormatView
所以总而言之,我需要能够将以下内容从我的 Controller 传递给碧 Jade 报告:
这就是我所拥有的,输出是一个空白的 PDF 文档,我假设是因为它不知道如何运行查询
@RequestMapping("/reports/**")
@Controller
public class ReportsController {
@RequestMapping(value ="/reports/usage/report", method = RequestMethod.GET)
public ModelAndView handleSimpleReportMulti(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("Made it here");
Map model = new HashMap();
//model.put("format", "pdf");
model.put("START_DATE", new String("09-12-2011"));
model.put("END_DATE", new String("09-17-2011"));
return new ModelAndView("report1", model);
}
}
最佳答案
我找到了我的问题的答案。我已将上面的 Controller 更改为:
@RequestMapping(value ="/reports/usage/report/{format}", method = RequestMethod.GET)
public ModelAndView handleSimpleReportMulti(ModelMap modelMap, @PathVariable("format") String format) throws Exception {
//Map model = new HashMap();
modelMap.put("format", format);
modelMap.put("REPORT_CONNECTION", dataSource.getConnection());
modelMap.put("START_DATE", new String("09-12-2011"));
modelMap.put("END_DATE", new String("09-17-2011"));
return new ModelAndView("report1", modelMap);
}
我已将 view.properties 更改为:
#report1(class)=org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView
report1(class)=org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView
report1.url=/WEB-INF/reports/report1.jasper
我希望这有帮助。
谢谢
关于spring-mvc - 如何将数据库连接信息和查询参数从 Controller 传递到 JasperReportsMultiFormatView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7735288/