Any report implemented in JasperReport / designed in iReport editor may contain only one source query to get data — defined in <queryString> element. In some case you need to obtain data from two or more queries. In such case is having one report not sufficient — to insert other reports into main template there are subreports available. Simple connect other JRXML file, share connection, parameters, … When deploying on JasperServer, the usage of subreports is slightly more complicated because of setting path to other template.

I have introduced how to work with JasperServer in a previous tutorial. We will need to report files to illustrate subreport usage: the main file MainReport.jrxml which generates tabular data and other report file, SubReport.jrxml, which shows one line with a value from database. Each of reports has its own parameters, settings and it is possible to run it independendly in editor. We will deploy them on server from iReport using JS plugin — select a folder to deploy the file, right-click on it and deploy the main report using Add > Report Unit, respectively subreport using Add > JRXML Document. We will not run the second file, but include in main report.

When having both templates deployed, open the main JRXML file file. In order to insert the second template use the Subreport button from toolbar and somewhere in report, e.g. in <title> section, create a space to render it. A wizard will open. Select Just create the subreport element option and finish the wizard; we will set its attributes in properties: in the Subreport tab we have to setup database connection. In the Connection/Data Source Expression select menu we will choose Use connection expression and as a expression we will set $P{REPORT_CONNECTION}, which will share the connection from main report to the subreport. Another important tab is the Subreport (Other) where you can set the path to the subreport template. Set java.lang.String as a Subreport Expression Class and put absolute path to the template into Subreport Expression. The pseudo–protocol "repo" is important as well! If you have the subreport template deployed on JasperServer into "(Root) / Subreports" folder, the path should be (include quotes as it is a Java String expression):

"repo:/Subreports/SubReport.jrxml"

Now you should see the subreport content directly in main report.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed

This blog copyright 2009 by Josef Petrak