After years of struggling with report design (using jasperreports and ireport) for web applications, I finally set up a method for fast development of reports. I was loosing lots of time changing and testing the reports (after each report compilation i had to recompile and redeploy my web application, then to access the web page containing the report).
The ideea: to have the report input in a file, to set it as an xml datasource in iReport and to run the report on real test data in iReport interface.
My environment: java 1.5, iReport 2.0.2 (compatible with JasperReports 2.0.2)
1. Parameter values
First, all report parameters must have a default value, and the “Use as a prompt” option unchecked - so that the default value is used without a confirmation popup window at runtime.
2. Field values from an xml datasource
To set up an xml datasource, create an xml file like in the example below (the node that is repeating corresponds to a detail line of the report):
<?xml version=”1.0″ encoding=”UTF-8″?><ComandaX>
<Product>
<code>ITEM1001</code>
<description>Descriere produs</description>
<cond>1 buc</cond>
<quant>10</quant>
<unitprice>10.10</unitprice>
<totalprice>1010</totalprice>
<no>1</no>
</Product>
<Product>
<code>ITEM1002</code>
<description>Descriere produs 2</description>
<cond>1 buc</cond>
<quant>20</quant>
<unitprice>20.10</unitprice>
<totalprice>4040</totalprice>
<no>2</no>
</Product>
</ComandaX>
Then in iReport, add an xml datasource with the expression /ComandaX/Product (corresponding to the xml above - the path to the node that is repeating):
For this to work and the report to be filled with data, you must also fill for each field the description (the field description must match the field name):
3. Path to images
A trick is necessary to see report resources both in iReport and in production environment. For a web application, a resource loaded from classpath is named relatively, for instance: “/../../resources/logo.jpg” (always with a leading slash). In iReport environment the path to the image can not contain a leading slash (for instance “/opt/resources/logo.jpg”. One solution: to define a report parameter that has default value “/opt/resources/logo.jpg” (used in iReport) and initialized in production environment with another value (for instance “/../../resources/logo.jpg”), and to set the image path using this parameter.
4. Enjoy!
Set the datasource created above as the default datasource and run the report (Execute with active connection). Report changes will take seconds, report designs will only take minutes Also, it is much easier to test multiple cases (larger reports for instance), and to develop reports independently (in time, resources) than the rest of the application.
Full example (report template, pdf report, xml datasource)
0 comments:
Post a Comment