Single object (java bean) as a data source for iReport (JasperReports)

I am new to iReport and I need to create a report in PDF format.

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

How can i do this? I'm a little confused, do I need to pass an array with only one element?

+6
source share
3 answers

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

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()); 
+14
source

If you want to use the predefined Jasper report classes, then yes. Standard JRAbstractBeanDataSource implementations JRAbstractBeanDataSource limited to processing arrays or collections. Thus, you can simply create an array from one element or one element.

An alternative would be to implement your own version of JRAbstractBeanDataSource , which drowns out the next() and moveFirst() methods, but it seems like dumb work when there is a direct way to get the required behavior.

+1
source

Even if you only have one bean, you can still use the collection data source. In this case, the collection will have only 1 object. If you create a report project with the correct grouping, this report can later be used to display several classes with their corresponding properties.

0
source

Source: https://habr.com/ru/post/907065/


All Articles