May 2, 2009
Dance with Dynamic Jasper Report

jasper report
I will show you step by step how to use Dynamic Jasper Report with custom query. I won’t show how to use static reportable in Domain because there are lots of tutorial like this one
Installing Plugins
It is as simple typing
grails install-plugin dynamic-jasper
Generating Domains step
We create 2 domains name: Book and Author simply type
grails create-domain-class Book
grails create-domain-class Author
and add some field on the domains :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Book { String name ; String barcode ; String summary; Author author; static belongsTo = [author:Author]; static constraints = { name(nullable:false, blank:false); barcode(nullable:false, blank:false); summary(nullable:false, blank:false); } String toString() { "${name}" } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class Author { String name ; String summary ; static hasMany = [books:Book]; static constraints = { name(nullable:false, blank:false); summary(nullable:false, blank:true); } String toString() { "${name}"; } } |
Create a controller for displaying the Jasper
grails create-controller Report
Here we want to create an excel report so we fill ReportController with this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import net.sf.jasperreports.engine.*; import net.sf.jasperreports.engine.data.*; import net.sf.jasperreports.engine.export.*; import net.sf.jasperreports.engine.util.*; import net.sf.jasperreports.view.*; import ar.com.fdvs.dj.domain.DynamicReport; import ar.com.fdvs.dj.domain.builders.FastReportBuilder; import ar.com.fdvs.dj.core.DynamicJasperHelper; import ar.com.fdvs.dj.core.layout.ClassicLayoutManager; import ar.com.fdvs.dj.output.*; class ReportController { def index = { FastReportBuilder drb = new FastReportBuilder(); drb = drb.addColumn("Book Name","name",String.class.getName(),30); drb = drb.addColumn("Barcode","barcode",String.class.getName(),20); drb = drb.addColumn("Author","author.name",String.class.getName(),30); drb = drb.addColumn("Author Summary","author.summary",String.class.getName(),70); DynamicReport dr = drb.setTitle("My tester ").setUseFullPageWidth(true).build(); JRDataSource ds = new JRBeanCollectionDataSource(Book.list()); JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds); def reportFormat = 'XLS'; def reportFileName = "myReport"; ReportWriter reportWriter = ReportWriterFactory.getInstance().getReportWriter(jp, reportFormat, [(JRHtmlExporterParameter.IMAGES_URI): "${request.contextPath}/report/image?image=".toString()]); if (reportFileName) { response.addHeader('content-disposition', "attachment; filename=${reportFileName}.${reportFormat.toLowerCase()}") } reportWriter.writeTo(response) } } |
FastReportBuilder will create columns and do what the jrxml does.
DynamicReport will build the report for you (i think it identical with compiling jrxml to .jasper)
You fill data of the report using JRBeanCollectionDataSource. here we use simple query Book.list(). You might try it using Criteria as well.
To display the report in the browser, we just tell the browser about the header and its content by this code :
ReportWriter reportWriter = ReportWriterFactory.getInstance().getReportWriter(jp, reportFormat, [(JRHtmlExporterParameter.IMAGES_URI): "${request.contextPath}/report/image?image=".toString()]); if (reportFileName) { response.addHeader('content-disposition', "attachment; filename=${reportFileName}.${reportFormat.toLowerCase()}") } reportWriter.writeTo(response)</blockquote>
done … that’s so easy.

dynamic jasper result in excel format
more information about dynamice jasper visit:
http://dynamicjasper.sourceforge.net/