Create SubReports - JasperReports

What are Subreports in Jasper Reports?

A report is incorporated within another report in the sense that one report is a subset of another report, which is known as Subreport. Any number of simple subreports can be created and encapsulated into a master report. The compilation and filling of subreports is similar to that of normal reports. When the subreport is incorporated into another report any report template can be used.

The subreports are net.sf.jasperreports.engine.JasperReport objects, which are the outcome after compiling the object.

subreport Element

When a subreport is entered into a master report, the <subreport> element is used. The sub-elements in <subreport> JRXML element is as follows:

  • <reportElement>
  • <parametersMapExpression> − The report parameters are passed into the sub report by this attribute. By using the built-in REPORTS_PARAMETERS_MAP parameter, the map can be obtained from the master report parameter. A java.util.Map object is returned in which the parameter names are the keys.
  • <subreportParameter> − The parameter is passed to the subreport by using this element having a mandatory attribute called Name.
  • <connectionExpression > − The java.sql.Connection is passed to the subreport by using this attribute. When a database connection is required by the subreport template during the report filling phase.
  • <dataSourceExpression> − A datasource is passed to the subreport by this attribute. The parameter is passed to the master report using the built-in REPORT_DATA_SOURCE parameter.
  • <returnValue> − The value of variables of one of the subreports is assigned to the variables of the master report by this attribute. The subreports variable, tovaribale , calculation, and incremental factor class are attributes of this sub element.
  • <subreportExpression> − The compiled report template for the subreport is identified by this subelement. Any one of the following can be taken by the class attribute from among values:java.lang.String,,,, net.sf.jasperreports.engine.JasperReport. Default value is java.lang.String.
  • isUsingCache – This sub element is a Boolean and when it is set to true, the previously loaded subreport objects are recognized by this element by using the specified source.


Consider an illustration of creating subreports by using JRDataSource. Two report templates are written one for subreports and the one for Master report. The contents of the subreport address_report_template.jrxml is saved to C:\tools\jasperreports-5.0.1\test directory and is given below:

A corresponding POJO file is written which is saved to the C:\tools\jasperreports-5.0.1\test\src\com\wisdomjobs directory:

Two fields 'city' and 'street' are declared by defining the getter and setter methods.

The existing DataBean file is updated and a new field subReportBeanList, is added. The list of the SubReportBean objects are holded by the field. The contents are as follows which are saved to the C:\tools\jasperreports-5.0.1\test\src\com\wisdomjobs directory.

The file C:\tools\jasperreports-5.0.1\test\src\com\wisdomjobs\ is updated. The contents of this file are as −

The list of the subreport bean is set in the produce() method.

A new master report template (jasper_report_template.jrxml) is written which is saved to the C:\tools\jasperreports-5.0.1\test directory. The contents of the file are:

A new parameter "SUBREPORT_DIR," is defined by the above template defining the path of the subreport. A field subReportBeanList is defined of the java.util.List type which is corresponding to the DataBean file. The subReportBeanList is put in an instance of the JRBeanCollectionDataSource. The subreport is named as (AddressReport.jasper).

A new class CreateReport is written and the report template is compiled and executed. The contents of file C:\tools\jasperreports-5.0.1\test\src\com\wisdomjobs\ are as follows:

The master and the sub report templates are compiled and the master report (.jasper) file is passed for the report filling.

How the report is generated using Subreports in Jasper Reports?

All the files are compiled and executed using the ANT build process. The contents of the file build.xml are saved under directory C:\tools\jasperreports-5.0.1\test as follows. The import file is to be placed in the same directory as build.xml which is extracted from the chapter Environment Setup

Open the command line window and go to the directory where build.xml is placed. Execute the command ant -Dmain-class=com.wisdomjobs.CreateReport

The compilation of the above code results in a JasperViewer window that appears as follows:

Subreport Example

It is observed that the Name, Country, and Address attributes are displayed.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

JasperReports Topics