Google

Jul 5, 2013

JasperReports 5.0 tutorial with iReport 5.0 - part 2



The part 1 was a comprehensive beginner style  Jasper Reports tutorial. This extends that to demonstrate how to pass parameters via a Map using name/value pairs to the Jasper Reports.


Step 1: Add a new custom parameter named "footerText" to display some value in the page footer section. Right-click on "Parameters" and select "Add Parameter" and then rename it to "footerText" as shown below. Also, note than the Text Field in the page footer is mapped to $P{footerText} by right clicking on the "Text Field" and then select "Edit expression" to select the parameter "footerText" from the list.





Step 2: Compile this to the jrxml file and then you can pass the value for "footerText" via Java as shown below in the Main.java file. Take note of the getParameters( ) method.

package com.mycompany.app.jasper; import java.io.IOException; import java.io.InputStream; import java.util.Collection; import java.util.LinkedList; import java.util.List; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.xml.JRXmlLoader;
package com.mycompany.app.jasper;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

public class Main
{
    
    public static JasperDesign jasperDesign;
    public static JasperPrint jasperPrint;
    public static JasperReport jasperReport;
    public static String reportTemplateUrl = "person-template.jrxml";
    
    public static void main(String[] args) throws IOException
    {
        try
        {
            InputStream resourceAsStream = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream(reportTemplateUrl);
            //get report file and then load into jasperDesign
            jasperDesign = JRXmlLoader.load(resourceAsStream);
            //compile the jasperDesign
            jasperReport = JasperCompileManager.compileReport(jasperDesign);
            //fill the ready report with data and parameter
            jasperPrint = JasperFillManager.fillReport(jasperReport, getParameters(),
                    new JRBeanCollectionDataSource(
                            findReportData()));
            //view the report using JasperViewer
            JasperViewer.viewReport(jasperPrint);
        }
        catch (JRException e)
        {
            e.printStackTrace();
        }
    }
    
    private static Collection findReportData()
    {
        //declare a list of object
        List<Person> data = new LinkedList<Person>();
        Person p1 = new Person();
        p1.setFirstName("John");
        p1.setSurname("Smith");
        p1.setAge(Integer.valueOf(5));
        data.add(p1);
        return data;
    }
    
    private static Map<String, Object> getParameters()
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("footerText", "Just to demonstrate how to pass parameters to report");
        return params;
    }
    
}


Step 3: Finally, run the Main.java to see the value "Just to demonstrate how to pass parameters to report" in the report footer. The parameters are handy to pass any arbitary name/value pairs to the report.





Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home