JasperGrails Tutorial
Author : Marcos Fábio Pereira (marcos [dot] at [dot] embedded.ufcg.edu.br)Contributers : Egon Jensen, João Paulo, Vinícius Cavalcanti, Craig Jones, Aaron Eischeid
Index
- Releases
- Step-by-step guide
- FAQ
- Issues
Releases
Version 0.9.5
release 23-Jan-2009New features:
- New tags which allow for greater control of JasperForms layout and button placement, and unobtrusive JS and CSS. see demo.gsp for documentation
- Icons rendered via CSS and new small PDF icon included
- Can now use latest iReport to edit reports thanks to updated Jasper libraries
Version 0.9 (Thanks to Craig Jones)
(Pending release as of 16-Aug-2008, so obtain from subversion trunk)New features:
- Added many new attributes to <g:jasperReport> that control the rendering (see demo.gsp)
- Now, if the g:jasperReport tag does not have a body, then the rendered HTML will be just a series of one or more <A> tags (no form and no javascript for submitting said form).
- Combined the redundant admin.gsp and howto.gsp into a single demo.gsp.
- Added lots more documentation and examples to demo.gsp.
- The Format attribute is now tolerant of lower case format values (pdf vs. PDF) and spaces between values.
- The tag validation code now checks for the two required attributes: jasper and format
- Rendering now uses and <strong> (rather than nothing and <b>) in places
- No longer renders the word "null" when the name attribute is left blank
- Numerous. (See subversion log.)
Version 0.8
New features:
- Attribute 'from' was removed.
- Attributes 'controller' and 'action' was included.

Version 0.7.7 (Thanks to Achim Breunig)
New features:
- Attribute 'inline=<boolean>' was included in jasperReport tag. Now, pdf-files can be shown inline in the web-browser.
- Bug fixed:
- Solved problem that the 'format'-attribute did not accept spaces.
- Solved problem that sometimes the from-parameter was not found.
- The default locale of the report is the request locale.
- The default subreport folder is the same of the report.
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
[...]<div class="paginateButtons"><g:paginate total="${Race.count()}" /> </div> <g:jasperReport jasper="all-races" format="PDF" name="All Races" /></div></body></html>
grails run-app
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>
<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>
FAQ
Can i get the report data from domain classes, instead of a sql query?
Yes, the user can retrieve data to the report directly from domain classes, without using SQL queries in the .jrxml file. It's only necessary to set the 'controller' and 'action' attributes in jasperReport tag, like showed bellow:
<g:jasperReport controller="my-controller" action="my-action" jasper="all-races" format="PDF" name="All Races" />
<MODEL_DATA> = Domain-Class.list() or
<MODEL_DATA> = ['field1':value1,'field2':value2] or
<MODEL_DATA> = anything you want that returns a map or a list of map...chain(controller:'jasper',action:'index',model:[data:<MODEL_DATA>],params:params)
def races = Race.getAll(23,25,26)
races.each{
println it.sposor.address
}
chain(controller:'jasper',action:'index',model:[data:races],params:params)<field name="sponsor.address.city" class="java.lang.String"/> … … <textFieldExpression class="java.lang.String"> <![CDATA[$F{sponsor.address.city}]]> </textFieldExpression>
<jasperReport … language="groovy" … >Can i use sub reports with Jasper Plugin?
Yes, if you put the sub report file in the same folder of the parent report file "APP-FOLDER/web-app/plugins/jasper-x.x/reports/" then it should work fine.But, if you want to put the sub report file in another folder, you've to add the SUBREPORT_DIR as a parameter to your jasperReport tag.Thanks to Achim Breunig.note: subreports have proven to be quite troublesome for some users, and we are working on ironing these issues out for the next release. in the meantime here is some random stuff that might be helpful.
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false"/> … <field name="races" class="java.util.Collection"/>
<subreport isUsingCache="true"> <reportElement key="subreport-1" x="17" y="362" width="387" height="89"/> <dataSourceExpression> <![CDATA[new JRBeanCollectionDataSource($F{races})]]> </dataSourceExpression> <subreportExpression class="java.lang.String"> <![CDATA[$P{SUBREPORT_DIR} + "/jasper-test_subreport1.jasper"]]> </subreportExpression></subreport>
Sub Report Work arounds
// For Local Grails Server File folder = new File("."); String folderString = folder.getAbsolutePath().toString(); folderString = folderString.substring(0, folderString.length()-2);// For Tomcat Deployment if (folderString.endsWith("/bin")) { folderString = folderString.substring(0, folderString.length()-4) + "/webapps/{APP_NAME}" }// Reset SUBREPORT_DIR parameters.SUBREPORT_DIR = folderString + "/WEB-INF/" + parameters.SUBREPORT_DIR;
Can i use JFree Chart with Jasper Plugin?
Yes, if you put the needed jars in the library folder of your grails installation. The needed jars are "jcommon-1.0.14.jar" and "jfreechart-1.0.11.jar".Thanks to Srimal Jayawardena, he has posted an entry with this solution [here].
Issues
- New tags require Prototype library
- Deviations from default URL mapping cause problems (fixed for next release)
- Sub-reports use is buggy/ inconsistent




