Child pages
  • JasperReports Tutorial

Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3


We start by defining a simple Person POJO class.

Code Block
package com.meviproacme.test;

public class Person {

    private Long id;

    private String name;

    private String lastName;

    public Person() {

    public Person(String name, String lastName) { = name;
        this.lastName = lastName;

    public Person(Long id, String name, String lastName) { = id; = name;
        this.lastName = lastName;

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;

    public String getName() {
        return name;

    public void setName(String name) { = name;


We need the following files:

  • dist/jasperreports-X.jar
  • lib/commons-*.jar (all the commons - except maybe for commons-logging)
  • lib/itext-X.jar
  • lib/jdt-compiler.jar

Copy these jars over to your S2_WEBAPP/WEB-INF/lib directory, and add them to your classpath.

Creating the Action

Code Block
package com.meviproacme.test.action;

import java.util.ArrayList;
import java.util.List;

import net.sf.jasperreports.engine.JasperCompileManager;

import com.meviproacme.test.Person;
import com.opensymphony.xwork.ActionSupport;

public class JasperAction extends ActionSupport {

    /** List to use as our JasperReports dataSource. */
    private List<Person> myList;

    public String execute() throws Exception {

        // Create some imaginary persons.
        Person p1 = new Person(new Long(1), "Patrick", "Lightbuddie");
        Person p2 = new Person(new Long(2), "Jason", "Carrora");
        Person p3 = new Person(new Long(3), "Alexandru", "Papesco");
        Person p4 = new Person(new Long(4), "Jay", "Boss");

        // Store people in our dataSource list (normally they would come from a database).
        myList = new ArrayList<Person>();

        // Normally we would provide a pre-compiled .jrxml file
        // or check to make sure we don't compile on every request.
        try {
        } catch (Exception e) {
            return ERROR;

        return SUCCESS;

    public List<Person> getMyList() {
        return myList;


Code Block
<package name="default" namespace="/" extends="jasperreports-default">
    <action name="myJasperTest" class="com.meviproacme.test.action.JasperAction">
        <result name="success" type="jasper">
            <param name="location">/jasper/our_compiled_template.jasper</param>
            <param name="dataSource">myList</param>
            <param name="format">PDF</param>


Code Block
<action name="myJasperTest" class="com.meviproacme.test.action.JasperAction">