Tuesday, August 5, 2008

Jasper Reports Grails Plugin - sub-reports

Here's something that tripped me up when developing my first JasperReport containing a sub-report for use with the JasperReport grails plugin. In order for the sub-report to resolve correctly you need to add a parameter to your parent report called SUBREPORT_DIR. This is then set by the JR plugin, and makes everything work as expected. When specifying the sub-report location, set it to
$P{SUBREPORT_DIR}  + "mysubreport.jasper"
(assuming your subreport is in the same directory as the parent report and called mysubreport.jasper) Don't forget to give the parameter a default value of empty string so that it still works when previewing in iReport. You also need to remember that the plugin runs the compiled versions (*.jasper not *.jrxml) so you need to compile the reports with iReport before running the application.

9 comments:

cinod said...

Thanks. Ive tried subreports with grails jasper plugin but it returns a "nullpointer exception".

It some how works when i preview it in Ireport thou.

Lee Butts said...

Did you see my other post: http://www.leebutts.com/2008/08/jasper-reports-grails-plugin-gotcha.html ?

Check that your iReport version matches the plugin.

cheers

Lee

cinod said...

Thanks. Your right, i was building on 3.0.1 all along.

Rilisema said...

Hi, did you already try to send non-String parameters?
I'm trying to send a java.util.Date parameter from my controller.
In the controller method the parameter is a java.util.Date, but in the JasperController it's suddenly a String. The report throws a net.sf.jasperreports.engine.JRException: Incompatible java.lang.String value assigned to parameter xxxx exception.

Any ideas?

Churk Leung said...

I have been trying to use this sub report function. While working under grails localhost, the way you describe to call sub report works great. But have to try creating a war then deploy it to tomcat. The way you describe doesnt work anymore.

Lee Butts said...

Hi Churck,

make sure the compiled .jasper files are being put into the war correctly.

What is the error you're getting?

I worked on this a while ago and can't really remember the exact workings of SUBREPORT_DIR. Perhaps it needs to be set to an absolute path so it works when running inside your application server.

cheers

Lee

RR said...

Lee, have you had success with subreports in Grails 1.2x? I've been looking around and reading various posts and suggestions but it appears that either I'm doing SUBREPORT_DIR wrong or this approach doesn't work anymore.

Lee Butts said...

Hi,

I haven't used the Jasper plugin on any 1.1 applications unfortunately so it is possible that this approach no longer works.

I think I discovered this way with some good old fashioned debugging so that may be worth a shot using 1.2 so see what the plugin is doing.

cheers

Lee

RR said...

Thanks for the info, Lee. I've been stepping through the Jasper plugin quite a bit. In fact, 3.5.2 is now out and I've added a Jira ticket to update the libraries, for the time being I'm rolling my own locally. Going forward I'll use this as a reference http://www.nabble.com/Re%3A-Alternatives-to-Jasper-Reports--p25120572.html
for my next attempts.