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 net.sf.jasperreports.engine.design.JasperDesign object.
When a subreport is entered into a master report, the <subreport> element is used. The sub-elements in <subreport> JRXML element is as follows:
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 SubReportBean.java 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\DataBeanList.java 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\CreateReport.java are as follows:
The master and the sub report templates are compiled and the master report (.jasper) file is passed for the report filling.
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:
It is observed that the Name, Country, and Address attributes are displayed.
JasperReports Related Interview Questions
|Java Script Interview Questions||Adv Java Interview Questions|
|Qlik View Interview Questions||Hibernate Interview Questions|
|Finacle Interview Questions||SSRS(SQL Server Reporting Services) Interview Questions|
|Business Objects-Crystal Reports Interview Questions||Framework7 Interview Questions|
|Java collections framework Interview Questions||Java 8 Interview Questions|
|iReport Interview Questions||Structured Query Report (SQR) Interview Questions|
|Qlik Sense Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.