JasperGrails Tutorial
Author : Marcos Fábio Pereira (marcos [dot] at [dot] embedded.ufcg.edu.br)
Contributers : Egon Jensen, João Paulo
Index
- Releases
- Step-by-step guide
- FAQ
Releases
Version 0.7.6
New features:
The user can to retrieve data report from domain classes:
Attribute 'from' was included in jasperReport tag.
XLS parameters were improved: (Thanks to Sebastian Esch)
One page per sheet;
Auto detect cell type;
Remove empty space between rows;
White page background is disabled.
Bug fixed:
Plugin didn't work on Linux (File separator was wrong). (Thanks to Sebastian Esch)
Version 0.7.5
Bug fixed:
bug in JasperService.groovy that can cause connection leaks, connection is never closed. (Thanks to Pass F. B. Travis)
----
Step-by-step guide
This guide shows how to call JasperReports from within Grails.
Written by Egon Jensen and Marcos Fábio Pereira.
I liked the Racetrack application in Getting Started with Grails, but I'm missing something. Wouldn't it be nice if you were able to create a report with all the races in, say, PDF format?
Don't worry; JasperReports can do that, and it is easy to use in Grails.
To follow this tutorial, you first have to create the Racetrack application from Getting Started with Grails. But of course you can use your own application, just remember to do the necessary changes as you follow along.
First you have to install JasperGrails
grails install-plugin jasper
Now modify "Racetrack/grails-app/views/race/list.gsp"
[...] <div class="paginateButtons"> <g:paginate total="${Race.count()}" /> </div> <g:jasperReport jasper="all-races" format="PDF" name="All Races" /> </div> </body> </html>
That's right; all we need is one line ![]()
Now start the application
grails run-app
Open a browser, and go to http://localhost:8080/racetrack
Logon and below the Race List, you'll now find a PDF icon.
Press the icon, and ...
Ups!
We forgot the JasperReports file ![]()
As you can see in the error message, we are supposed to create and save "all-races.jasper" in "racetrack/web-app/plugins/jasper-0.6/reports/". After doing that, use the browsers back button and try the PDF icon again. This time it works better.

Open the file and you get a beautiful report with all the races.

OK, I admit it, JasperReports (and iReports) is not my strong side.
Besides, this is a tutorial for JasperGrails, not for JasperReports ![]()
Let's try another example.
This time we will export all the registrations for a given race to Excel. Again we start with modifying the view: "racetrack/grails-app/views/race/show.gsp"
[...] <div class="buttons"> <g:form> <input type="hidden" name="id" value="${race?.id}" /> <span class="button"><g:actionSubmit class="edit" value="Edit" /></span> <span class="button"><g:actionSubmit class="delete" onclick="return warnBeforeRaceDelete();" value="Delete" /></span> </g:form> </div> <g:jasperReport jasper="registrations" format="XLS" name="Registrations"> <input type="hidden" name="race_id" value="${race.id}" /> </g:jasperReport> </div> </body> </html>
We want to pass an argument to JasperReport, and we can do just that with a hidden field. Remember to include the JasperReports file.
This time we will try with the jrxml format. Thus we save "registrations.jrxml" in "racetrack/web-app/plugins/jasper-0.6/reports/".
Remember to include the following in "registrations.jrxml", to accept the argument we send from "show.gsp":
<parameter name="race_id" isForPrompting="false" class="java.lang.String"> <defaultValueExpression><\!\[CDATA\["<parameter error>"\]\]></defaultValueExpression> </parameter> <queryString><\!\[CDATA\[select * from registration where race_id = $P{race_id}\]\]></queryString>
Click on "Turkey Trot", and below Show Race, we now have an Excel icon.
Press the icon and open the resulting file.
But, if we don't want to use a SQL query to generate the report? If we want to use domain classes?
The user can also to retrieve data to the report directly from domain classes, without use SQL queries in .jrxml file. It's need only to set the 'from' attribute in jasperReport tag, like showed bellow:
<g:jasperReport from="Race.list()" jasper="all-races" format="PDF" name="All Races" />
It's correct! The 'from' attribute must be without '${}'. The code inside it will be evaluate by the plugin, when the user click in the icon.
Now, the .jrxml file can be defined without a datasource. The only thing you have to remember is to set the name of all fields in the report with the same name of the attributes in the domain class.

Comments (9)
Apr 03, 2008
Robert O'Connor says:
Nice job!Nice job!
Jul 04, 2008
alessandro benedetti says:
Hi, many thanks for your job!!! Unfortunately i always obtain this...Hi, many thanks for your job!!!
Unfortunately i always obtain this error:org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NullPointerException
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:92)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:226)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:899)
at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:740)
at groovy.lang.Closure.call(Closure.java:292)
at groovy.lang.Closure.call(Closure.java:287)
....
The code i've added into the list.gsp is the following:
.....
</div>
<g:jasperReport from="Race.list()" jasper="all-races" format="PDF" name="All Races" />
</div>
</body>
</html>
The version i'm using of iReport is 3.0.0
Can you help me ?
Many thanks
Alessandro
Aug 12, 2008
Espen Schulstad says:
Hi, I'd like to know how to change the apparence of the tag. Could you point me ...Hi, I'd like to know how to change the apparence of the tag. Could you point me in the right direction?Great work, I really appreciate it!Sorry. I'm kind of new at this. Found it out
Sep 04, 2008
Jeff Self says:
I just installed the jasper plugin and I'm getting a Grails Runtime Exception.&n...I just installed the jasper plugin and I'm getting a Grails Runtime Exception. Here it is:
Message: Unknown Report File: C:\Documents and Settings\jself\My Documents\Projects\Grails\racetrack\web-app\plugins\jasper-0.8\reports\/\all-races.jasper or .jrxml
Caused by: java.lang.Exception: Unknown Report File: C:\Documents and Settings\jself\My Documents\Projects\Grails\racetrack\web-app\plugins\jasper-0.8\reports\/\all-races.jasper or .jrxml
Class: JasperService
At Line: [79]
Sep 25, 2008
Stefan Kunz says:
I get the same NullPointerException as Alessandro Benedetti with Grails 1.0.3 an...I get the same NullPointerException as Alessandro Benedetti with Grails 1.0.3 and iReport 3.1.1. Preview of the report works fine in iReport.
Oct 18, 2008
Adolfo Ernesto Custidiano Secchi says:
Hi i have a fix to the jasper taglib. What i have to do to contribute th...Hi
i have a fix to the jasper taglib.
What i have to do to contribute this feature to the project?
Regards.
Dec 14, 2008
Mariano Stampella says:
Hi, Im working in an article related with DynamicJasper and Grails, i wan...Hi,
Im working in an article related with DynamicJasper and Grails, i want to shared with you, how i can do that?
Regards,
Marian.
mstampella@gmail.com
Apr 28
sergio muñoz says:
Hi.. Im working with Grails 1.1 and Jasper plugin 0.9.5, following all steps I h...Hi.. Im working with Grails 1.1 and Jasper plugin 0.9.5, following all steps I have
org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.Exception: No such report spec: reports\listado.jasper or .jrxml
I don´t undertand what happend, because in the reports directory exists a listado.jrxml
any ideas?
May 06
sergio muñoz says:
Well I have another question.... When I use the <g:jasperreport> tag ever...Well I have another question....
When I use the <g:jasperreport> tag everything works, when I create a report from java code the report is sucessfull created, but the visualization have a problem, a blank space into a cell appears, just when the data from the data base ends. The posible cause for this is the page height, but my question is, how I do to view the report like a report generated from the tag?