Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One object (java bean) as data source on iReport (JasperReports)

I'm new in iReport and I have to create a PDF report.

With a JavaBean DataSource you wrap an array (or collection) of JavaBeans but I only need to pass an object (only one instance of a JavaBean). I mean, I have to show in my report the properties of a java bean.

How can I do this? I'm a little bit confused, I have to pass an array with only one item?

like image 876
ilazgo Avatar asked Jan 27 '12 19:01

ilazgo


People also ask

Where do I put JasperReports properties?

jasper. properties is looked up by jasper reports in the classpath, so it can be directly in the WEB-INF/classes folder, or in the root folder of any of the jars in WEB-INF/lib.

What is the difference between Jrxml and Jasper?

jrxml is a human readable XML file that contains the report template i.e. report structure and its formatting rules. . jasper is the compiled report template i.e. compiled . jrxml file.


1 Answers

You can pass your bean to the report using a JRBeanArrayDataSource or JRBeanCollectionDataSource or you can use the parameters Map.

JasperPrint reportPrint = JasperFillManager.fillReport(
        this.getClass().getClassLoader().getResourceAsStream("/report.jasper"),
            new HashMap<String,Object>(), 
                 new JRBeanArrayDataSource(new YourBean[]{yourBean}));

or

Map<String,Object> params = new HashMap<String,Object>();
params.put("yourBean", yourBean);

JasperPrint reportPrint = JasperFillManager.fillReport(
        this.getClass().getClassLoader().getResourceAsStream("/report.jasper"),
                params, new JREmptyDataSource());
like image 124
dcernahoschi Avatar answered Sep 29 '22 21:09

dcernahoschi