Add Seam Report (JasperReport) to a JEE 6 Project - a simple example

For generating a pdf and printing some data, I decided to use JasperReports in my existing project. Because it is a Maven-based JEE 6 project, I wanted to give Seam Report a try. It allows to integrate different reporting tools like JasperReport, Pentaho or XDocReport. I use a simple Maven project from my other articel, you can download the project here.

Requirements(read here how to do this):

- you have installed Maven 3 or later and configured the JBoss Maven repository
- you have installed Eclipse with JBossTools
- you have installed the JBoss Applicationserver 7.0.2 and added the server runtime to Eclipse

You will need iReport to create the report, download it here.

Skip the following step, if you have already a JEE 6 project with CDI support.

1. Import example project

1.1 Extract the example project to a path at your harddisk.

1.2 Start Eclipse and import the project:

1.3 Choose File > Import > Maven > Existing Maven Projects and click next

1.4 Browse to the folder of the example project

1.5 Make sure the checkbox of the pom.xml is activated before you hit finish

2. Configure the project

Add this property to the pom.xml:


and the following dependencies:




3. Add PDF generation

3.1 Create a package org.example.util in the source folder and a class MemberReport

package org.example.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.example.data.MemberListProducer;
import org.jboss.seam.reports.Report;
import org.jboss.seam.reports.ReportCompiler;
import org.jboss.seam.reports.ReportDefinition;
import org.jboss.seam.reports.ReportRenderer;
import org.jboss.seam.reports.jasper.annotations.Jasper;
import org.jboss.seam.reports.output.PDF;
import org.jboss.solder.resourceLoader.Resource;

public class MemberReport {
    InputStream sourceReport;

    ReportCompiler compiler;

    JRDataSource jasperDataSource;

    ReportRenderer pdfRenderer;
    MemberListProducer memberListProducer;

    public ByteArrayOutputStream generateMemberReport() throws Exception {
        ReportDefinition report = compiler.compile(sourceReport);
        Map params = new HashMap();
        params.put("ReportTitle", "Member Report");
        Report reportInstance = report.fill(
          new JRBeanCollectionDataSource(memberListProducer.getMembers()), params);
        ByteArrayOutputStream os = new ByteArrayOutputStream(); 
        pdfRenderer.render(reportInstance, os);
        return os;
 public void createPdf() throws Exception {

     byte[] pdfData = generateMemberReport().toByteArray();
     FacesContext facesContext = FacesContext.getCurrentInstance();
     ExternalContext externalContext = facesContext.getExternalContext();
     HttpServletResponse response = 
              (HttpServletResponse) externalContext.getResponse();

                               "attachment; filename=\"memberReport.pdf\""); 

     OutputStream output = response.getOutputStream();


3.2 In src/main/webapp/index.xhtml add the following lines between dataTable and define at the end of the file


3.3 Open iReport choose File > New.. in the menu and select Blank A4, click Open this Template

3.4 Name it "memberReport" and browse to the resource folder of the project, i.e. /Users/myUser/Documents/workspace/JEE6Project/src/main/resources

3.5 Add the fields name, email and phoneNumber to the report

3.6 Drag and drop the fields to the detail area of the report

3.7 Create a parameter and name it "ReportTitle", drag it to the title area of the report. It should look like this:

3.8 Save and open the report in Eclipse. In the second line change language="groovy" to language="java"

3.9 In Eclipse right click the pom.xml and choose Run As -> Maven install

3.9 You should find the file JEE6Project.war in the target folder, right click it and select "Mark as Deployable". Choose a JBoss 7 runtime environment

Open http://localhost:8080/JEE6Project and click Print all members, the generated PDF should look like this:

Download the final project

Versions of software used:

Keine Kommentare:

Kommentar veröffentlichen