Dashboard > Grails > ... > Plugins > Jasper Plugin
Jasper Plugin Log In | Sign Up   View a printable version of the current page.

Added by Marcos Fábio Pereira , last edited by Marcos Fábio Pereira on May 09, 2008  (view change)
Labels: 
(None)

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.

FAQ


Site running on a free Atlassian Confluence Open Source Project License granted to The Codehaus. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.6.2 Build:#919 Nov 26, 2007) - Bug/feature request - Contact Administrators