Tuesday, December 21, 2010

Spring 3 MVC - Jasper: Sub-reports Tutorial

In this tutorial we will integrate Jasper reporting with a simple Spring 3 MVC application. We will create a master report and a sub-report to display our data. To design the report layout we will use iReport to create the template. If you're not familiar yet with integrating a simple report, I suggest you read my other tutorial: Spring 3 MVC - Jasper Integration Tutorial

What is Jasper Reports?
JasperReports is the world's most popular open source reporting engine. It is entirely written in Java and it is able to use data coming from any kind of data source and produce pixel-perfect documents that can be viewed, printed or exported in a variety of document formats including HTML, PDF, Excel, OpenOffice and Word.

Source: http://jasperforge.org/projects/jasperreports

What are Sub-reports?
Sub-reports are normal Jasper reports. They're called sub-reports because they are embedded within a report. The structure and data access is essentially the same.

Similar with our previous tutorial. We will setup our Spring 3 MVC application first. If you need a review with Spring MVC, I suggest you read the other tutorials I've posted. Or you can also Google for related tutorials.

The image below is an actual screenshot of our report without the sub-report. If you want to learn how to generate the report below, please visit the following link: Spring 3 MVC - Jasper Integration Tutorial


With sub-report added, here's the new image:

We added two extra fields. These two fields are located in the sub-report. These fields are place on the Summary section of the report layout. If you place these fields on the Detail band, the data will be repeated as many times as the number of rows in your datasource.

Using iReport we can generate both report layouts. On the final document, the master and sub-report are merged as if there's only a single document.

Here's the master report:

The highlighted gray rectangle indicates an embedded sub-report. The format is exactly the same as we had in our previous Jasper tutorial. If you need to a refresher, feel free to read that tutorial first.

Here's the sub-report:

Notice we declared two Parameters here: a LOCATION and MANUFACTURER parameter. I emphasized Parameters not Fields because those two terms are different. We can save ourselves some headache by just declaring these parameters in the master report directly, so that we don't have to create a sub-report. But where's the joy in that? We want to learn sub-report because it provides other benefits besides just adding simple parameters.

Here's the JRXML document for the sub-report:
tree-template_subreport1.jrxml

Here's the structure of our project:

Besides the Spring XML files, these are all the files we need for this project.

We can now begin our project.

Our first task is to setup the main controller that will handle the download request.

This controller declares three mappings:
/download - for showing the download page
/download/xls - for retrieving the actual Excel report
/download/pdf - for retrieving the actual PDF report

If you notice in each request handlers, we return an instance of ModelAndView. The view names are declared on a separate XML file under /WEB-INF/jasper-views.xml
jasper-views.xml

We have declared four beans in this XML file. Each bean uses a Spring built-in view for Jasper that corresponds to a specific format. Take note of these special properties for each view:
id
url
reportDateKey
subReportUrls
subReportDataKeys

Also take note of the following sub-report variables:
JasperCustomSubReportLocation
JasperCustomSubReportDatasource

We declared these two variables in the master JRXML report file named tree-template.jrxml.

First, in the headers:

Second, in the subreport element:

If you want to see the whole JRXML file, you can find it in the downloadable project at the end of this tutorial. If you look carefully at the following line:

We have basically wrapped our sub-report datasource JasperCustomSubReportDatasource with a JRBeanCollectionDataSource. This is important if you want to see your data repeated over multiple rows. Otherwise, all your data will be consumed by the first record. See this post at SpringSource forums for more info.

Let's examine further the contents of the subreport element:

Here we're referencing two sub-report parameters: LOCATION and MANUFACTURER. These parameters are declared inside the sub-report JRXML file itself. For each parameter, we declare how we populate this parameter. We're basically instructing Jasper "Hey, Jasper this is what you need to do when you encounter this parameter". For example, in the LOCATION parameter, we wrote:
[CDATA[$F{location}]]

This means take and assign the value of the Field location.

Take note that LOCATION and location are not the same in this tutorial. LOCATION is a Parameter that we declared in the sub-report file; whereas location is a Field we declared in the master report file.

If you examine the header of the master report file, you'll see the following field declarations:

The same thing goes for the parameter MANUFACTURER and the field manufacturer.

If you look back at the MainController, the master report and the sub-report share the same datasource. How does our datasource looks like?


Our datasource is a simple array list populated with simple POJO instances.

Sales


Our application is almost finished. We just need to setup some required Spring configuration files.

To enable Spring MVC we need add it in the web.xml

web.xml

Take note of the URL pattern. When accessing any pages in our MVC application, the host name must be appended with
/krams

In the web.xml we declared a servlet-name spring. By convention, we must declare a spring-servlet.xml as well.

spring-servlet.xml

By convention, we must declare an applicationContext.xml as well.

applicationContext.xml

Our application is now finished. We've managed to setup a simple Spring 3 MVC application with reporting capabilities. Thanks to Jasper. We've managed to add a master report and a sub-report successfully. We've also leveraged Spring's MVC programming model via annotation.

To access the download page, enter the following URL:
http://localhost:8080/spring-jasper-subreport-single/krams/main/download

If you want to download the report directly, enter the following URL for XLS format:
http://localhost:8080/spring-jasper-subreport-single/krams/main/download/xls

For PDF format, enter the following URL:
http://localhost:8080/spring-jasper-subreport-single/krams/main/download/pdf

For HTML and CSV, I left this exercise for my readers.

The best way to learn further is to try the actual application.

Download the project
You can access the project site at Google's Project Hosting at http://code.google.com/p/spring-mvc-jasper-integration-tutorial/

You can download the project as a Maven build. Look for the spring-jasper-subreport-single.zip in the Download sections.

You can run the project directly using an embedded server via Maven.
For Tomcat: mvn tomcat:run
For Jetty: mvn jetty:run

If you want to learn more about Spring MVC and Jasper, feel free to read my other tutorials in the Tutorials section.

For more info about Sub-reports, check the following:
Spring Framework Reference: Chapter 17.7 JasperReports
JasperForge: Subreports! by Giulio Toffoli
StumpleUpon DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring 3 MVC - Jasper: Sub-reports Tutorial ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share

8 comments:

  1. Hi,
    How to export this pdf to local.i tried exporttopdf and other ways for your example .can you help me in this

    Regards
    kent

    ReplyDelete
  2. Hi Krams,

    This tutorial was very useful.
    But I am facing problem in a situation where subreport will have another subreport in it.

    How can I solve it and Could you please share the links of documentations you read for jasper and spring.

    Thanks in advance :)

    ReplyDelete
    Replies
    1. Honestly, that's a pain indeed: a subreport within a subreport. I can't promise any but have you looked at my latest tutorial regarding Spring and Jasper. This one is kinda old. See my Tutorials section.

      Regarding documentation, I would say it blatantly, Jasper's documentation is outright bad. The forums aren't that helpful. Spring's forums are way better but the number of articles explaining Jasper and Spring are too rare. I would boldy say that my blog probably is where you can find most of the Spring and Jasper integration articles. Correct me if I'm wrong :-)

      Delete
    2. Thanks for the prompt reply.

      Please don't get me wrong but there are many tutorials in your blog(which is great!) and it's kinda hard to find the right one for my problem. Can you take out some time from your busy schedule and share the link for the appropriate tutorial/s.

      Thanks,
      Omkar

      Delete
  3. I cant get the JRXML to work. I hoped you expound how to make it in iReport even more. I get error with it in Eclipse.

    ReplyDelete
  4. Can you share one example of jasper report created from XML file which has child element also. It would be good to see this example done using Spring MVC and Jasper.

    Thanks,
    Ashish

    ReplyDelete
  5. I have read your blog its very attractive and impressive. I like it your blog.

    Spring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training

    spring training in chennai spring hibernate training in chennai

    ReplyDelete