Marky Mark
Marky Mark

Reputation: 98

Jasper Report with List or Collection of Pojos

This question is about Jasper Reports.

Suppose I want to create a Jasper Report using JRBeanCollectionDataSource like this ...

jasperPrint = JasperFillManager.fillReport(JRLoader.getInputStream(fileName), parameters, new JRBeanCollectionDataSource(aList));

And suppose that the PoJo I am using has a Property of type List.

How can I access

  1. The individual items in this List,
  2. and if this item is itself a Pojo access it's properties in Jasper Report XML file.

For example in the Jasper XML file:

...field name="?????" class="java.lang.String" ...

Thanks for your help ...

Upvotes: 2

Views: 11080

Answers (1)

mdahlman
mdahlman

Reputation: 9410

If your data source has fields of type List (more generally, of type java.util.Collection), then you can use a Table or List or Subreport component to iterate through them. Use one of those elements and set its data source like this:

new net.sf.jasperreports.engine.data.JRMapCollectionDataSource($F{myListField})

You could also add your own helper class with a static method to deal with these fields appropriately.

This comes up a lot with MongoDB. I wrote an article on Collections in JasperReports. It uses MongoDB as the data source... but it would apply equally well to your POJO data source.

Upvotes: 6

Related Questions