|Home > Tutorials > JasperReports Tutorial|
|The Struts 2 JasperReports plugin is a bridge from Struts 2 to JasperReports and does not include JasperReports itself, which must be downloaded separately.|
JasperReports is one of the leading open-source Java reporting libraries. It compiles .jrxml (XML source) to .jasper (compiled) files, which in turn can be transformed into several output types including PDF, HTML, CSV, and XLS.
In the following example, we will use the framework to create a PDF with a list of persons. Our action will be used to create a List with Person objects, and our JasperReports Result will use this list to fill our template, and return the PDF.
We start by defining a simple Person POJO class.
Before we can continue, we need to add the JR libraries to our classpath. You can download the JR project here: http://www.sourceforge.net/projects/jasperreports
Save the jasperreports-X-project.zip to your harddisk, and extract the files.
We need the following files:
Copy these jars over to your S2_WEBAPP/WEB-INF/lib directory, and add them to your classpath.
Our JasperAction creates a list of several People. The JasperCompileManager compiles the jrxml template to a .jasper file.
|Again, don't use this in production code. You should of course either provide compiled templates, or do some sort of checking to avoid compiling the template on every request. But for our demonstration, or development, this suits our needs just fine.|
JR uses XML configuration to define templates which are compiled to .jasper files. These templates define the resulting report.
This is a handwritten version - for more complex versions I seriously suggest taking a look a the various GUI designers.
Save this file in S2_WEBAPP/jasper/ as 'our_jasper_template.jrxml'.
Most important: we declared the fields name and lastName (two properties from our Person class). This means we will now be able to use these fields in our Jasper template.
We define two columnheaders (NAME and LASTNAME), and then add our fields to the detail band (for a better explanation, look at the JR tutorial). This 'detail' band will iterate over our List of People. This is the default behaviour of JR - so if you want to display more information from the Person, add them to this band.
In the detail band we use the
expression. JasperReports will ask Struts to retrieve the name field value from a Person object; the lastName field is handled the same way.
The rest is markup to define the layout.
|Use a logger (commons-logging, log4j, ...) to watch org.apache.struts2.views.jasperreports in debug mode, if you have any troubles.|
Using the JasperReports plugin requires adding the JasperReports result type as well as normal action configuration.
To use the JasperReports result type we must either (a) extend the jasperreports-default package that defines it or (b) manually define the JasperReport jasper result type ourselves.
In the above example we extend the jasperreports-default package; we can define the jasper result type manually by defining it the same way the JasperReport plugin does:
We configure our JasperAction with the name 'myJasperTest' - this means that we can execute this Action by sending a request to myJasperTest.action in our browser.
When our JasperAction executes correctly, we will use the Result type registered with the name 'jasper'. As discussed above the "jasper" result type is available from either extending the "jasperreports-default" package or by defining the result type manually.
The "location" parameter defines the location of the compiled jasper file, which will be filled by Struts 2 with our dataSource:
The "dataSource" parameter defines the action property containing the collection of objects to use in our report. In this case it's the myList property which we manually filled with some Person objects.
The "format" parameter specifies the output format of the report. Possible values include PDF, CSV, XLS and HTML.
You should now be able to execute http://localhost:8080/YOUR_WEBAPP/myJasperTest.action - and you should see a nice list of names.
Struts provides probably the most elegant way to deal with JasperReport files; specify the location of the .jasper file, specify what dataSource you want to use, and there you go.