一尘不染

如何在Spring MVC中使用JasperReports?

spring-mvc

我一直在研究将JasperReports(6.0.0)与Spring
MVC(4.1.3)结合使用以生成PDF报告。Spring充斥着与JasperReports集成以生成PDF的“特定于Spring”的方式:

我很难在网上找到好的完整示例,并想分享我的发现。

随意添加与“如何将JasperReports与Spring4集成”相关的其他方法和/或改进?


阅读 483

收藏
2020-06-01

共1个答案

一尘不染

根据我的研究,我发现了以下使用方法。这些方法从最直接的(天真的)方法开始,涉及较少的前期复杂性/配置,并逐渐演变为抽象的方法,但对Spring的依赖性更大/对Spring的配置更加复杂。

方法1:直接在控制器中使用JasperReports API

只需将内容写出到servlet输出流即可。

  @RequestMapping(value = "helloReport1", method = RequestMethod.GET)
  @ResponseBody
  public void getRpt1(HttpServletResponse response) throws JRException, IOException {
    InputStream jasperStream = this.getClass().getResourceAsStream("/jasperreports/HelloWorld1.jasper");
    Map<String,Object> params = new HashMap<>();
    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());

    response.setContentType("application/x-pdf");
    response.setHeader("Content-disposition", "inline; filename=helloWorldReport.pdf");

    final OutputStream outStream = response.getOutputStream();
    JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);
  }

方法2:将JasperReportPdf视图注入控制器

鉴于JasperReportsPdfView bean:

@Bean @Qualifier("helloWorldReport2")
public JasperReportsPdfView getHelloWorldReport() {
  JasperReportsPdfView v = new JasperReportsPdfView();
  v.setUrl("classpath:jasperreports/HelloWorld2.jasper");
  v.setReportDataKey("datasource");
  return v;
}

该视图可以注入或连接到Controller中以供使用:

@Autowired @Qualifier("helloWorldReport2")
private JasperReportsPdfView helloReport;

@RequestMapping(value = "helloReport2", method = RequestMethod.GET)
public ModelAndView getRpt2(ModelAndView modelAndView) {
  Map<String, Object> parameterMap = new HashMap<>();
  parameterMap.put("datasource", new JREmptyDataSource());
  modelAndView = new ModelAndView(helloReport, parameterMap);
  return modelAndView;
}

请注意,使用JasperReportsPdfView(或更通用JasperReportsMultiFormatView)需要依赖spring-
context-support:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>4.1.3</version>
</dependency>

方法3:使用XML或ResourceBundle视图解析器将逻辑视图名称映射到JasperReport视图

配置一个新的视图解析器,在这种情况下,ResourceBundleViewResolver将在之前运行InternalResourceViewResolver。这基于设置的订单值(0发生在1之前):

@Bean
public ResourceBundleViewResolver getResourceBundleViewResolver() {
  ResourceBundleViewResolver resolver = new ResourceBundleViewResolver();
  resolver.setBasename("jasperreport-views");
  resolver.setOrder(0);
  return resolver;
}

@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
  InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  resolver.setPrefix("/WEB-INF/views/");
  resolver.setSuffix(".jsp");
  resolver.setOrder(1);
  return resolver;
}

然后,在我们的类路径的根目录下,jasperreport- views.properties文件可以包含逻辑视图名称,以及与发送JasperReport相关的类和属性值(即url和reportDataKey)的配对:

helloReport3.(class)=org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView
helloReport3.url=classpath:/jasperreports/HelloWorld3.jasper
helloReport3.reportDataKey=myDataSourceKey

控制器代码如下所示:

@RequestMapping(value = "helloReport3", method = RequestMethod.GET)
public ModelAndView getRpt3(ModelMap modelMap, ModelAndView modelAndView) {
  modelMap.put("myDataSourceKey", new JREmptyDataSource());
  return new ModelAndView("helloReport3", modelMap);
}

我喜欢这种方法。控制器保持“哑”状态,仅处理字符串值,名称到视图的映射可以全部发生在一个位置。


方法4:使用JasperReportsViewResolver

配置零序JasperReportViewResolver,技巧是setViewNames用来告诉Spring您希望此解析器处理的逻辑视图名称(否则,您将得到“无法从类路径资源[jasperreports
/ index.jasper]加载JasperReports报告”类型错误):

@Bean
public JasperReportsViewResolver getJasperReportsViewResolver() {
  JasperReportsViewResolver resolver = new JasperReportsViewResolver();
  resolver.setPrefix("classpath:/jasperreports/");
  resolver.setSuffix(".jasper");
  resolver.setReportDataKey("datasource");
  resolver.setViewNames("rpt_*");
  resolver.setViewClass(JasperReportsMultiFormatView.class);
  resolver.setOrder(0);
  return resolver;
}

@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
  InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  resolver.setPrefix("/WEB-INF/views/");
  resolver.setSuffix(".jsp");
  resolver.setOrder(1);
  return resolver;
}

在控制器内部:

@RequestMapping(value = "helloReport4", method = RequestMethod.GET)
public ModelAndView getRpt4(ModelMap modelMap, ModelAndView modelAndView) {
  modelMap.put("datasource", getWidgets());
  modelMap.put("format", "pdf");
  modelAndView = new ModelAndView("rpt_HelloWorld", modelMap);
  return modelAndView;
}

这是我的首选方法。控制器以与使用jsp视图解析方式非常相似的方式解析jasper报告,InternalResourceViewResolver因此,与上述方法3中的xml或properties文件方法一样,不需要显式映射文件。

编辑

提及的JavadocsJasperReportsPdfView使用不推荐使用的JRExporterAPI。是否有更好的(较新的)JasperReports视图可以使用?也许选择s
JasperReportsMultiFormatView是更好的选择,因为它似乎没有使用JRExporter

2020-06-01