Saturday, August 13, 2011

Spring Jasper Integration

Jasper is a reort generating framework. It's quite powerful in terms of generating reports in different formats like html, csv, xls and pdf. The reports can be designed using iReports which is a GUI based tool. In this article, we will concentrate on how to integrate Jasper Report with Spring so that the reporting mechanism can be configured in the Spring way in the configuration file.

To integrate jasper with Spring let's first get the jasper dependency in the maven pom.xml


If you are using older version of Jasper, than check online documentation as Jasper repository has a different artifactId.

After pulling out dependencies, we have to register a View Resolver which will be resolving the views using a properties file. Make a file and put in the classpath.


myReort.(class) tells that which kind of report view to be used. The choices that are available are:

  • JasperReportsCsvView - Generates CSV file
  • JasperReportsHtmlView - Generates HTML file
  • JasperReportsPdfView - Generates PDF file
  • JasperReportsXlsView - Generates Excel file
  • JasperReportsMultiFormatView - The format is decided at runtime. It uses one of the above views to do the job.

myRport.url - tells the location of the file which contains the report format. jrxml is XML based file which Jasper understands to render the report. The jrxml files can be generated using iReport. jrxml files are compiled to jasper format before Jasper report can act on them to build the report. With spring integration one need not have to generate the jasper. Spring takes care of compiling jrxml to jasper.

myReport.reportDataKey - This registers the key against which the datasource will be available to fetch the report data. Against this key, one can insert the DataSource object itself or an instance of JRDataSource. The DataSource the connection to the database and the JRDataSource contains the data as part of the collection itself. We will see how it is done using the DataSource way. (JRDataSource way is explained in Spring reference document).

Now to fetch the report, we will inject the DataSource in a Controller object. In real application, you might want to use a Reporting Service so that the dataSource is not visible at Controller level.

public class ReportingController {
        //DataSource injected in the Reporting Service.
private DataSource dataSource;

public DataSource getDataSource() {
return dataSource;

        //Reporting mehtod in the controller 
        @RequestMapping(value = "/myReport/print", method = RequestMethod.GET)
        public String printMyReport(ModelMap modelMap) throws Exception     
        //Put the dataSource object against the  key as mentioned in
modelMap.put("dataSource", dataSource);
        //Add the type of report that needs to be generated. Right now it is hard coded
        //to pdf. But this can be easily fetched as a parameter from the request.
modelMap.addAttribute("format", "pdf"); 

        //Return the view key as mentioned in 
return "myReport";

To hook the report generation in the front page, we can put a link and map it to the request as outlined in RequestMapping.

<a href="/myReport/print" target="_blank">Print</a>

No comments:

Post a Comment