JavaBeans often contain collections of data. JRBeanCollectionDataSource allows you to display data from these collections in a variety of ways.
How can I display data from a java.util.List stored in a JavaBean in the Detail band of a Jasper report?
Key Considerations for Solution:
Java Class:
// JavaBean with a list of strings public class BeanWithList { private List<String> cities; private Integer id; // Getter methods }
JRXML:
<jasperReport> <subDataset name="dataset1"> <field name="city" class="java.lang.String"> <fieldDescription><![CDATA[_THIS]]></fieldDescription> </field> </subDataset> <field name="id" class="java.lang.Integer"/> <field name="cities" class="java.util.Collection"/> <title> <staticText>Bean with List sample</staticText> </title> <detail> <textField> <reportElement x="0" y="0" width="100" height="20"/> <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression> </textField> <componentElement> <reportElement x="100" y="0" width="400" height="20"/> <jr:list> <datasetRun subDataset="dataset1"> <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{cities})]]></dataSourceExpression> </datasetRun> <jr:listContents height="20" width="400"> <textField> <reportElement x="0" y="0" width="100" height="20"/> <textFieldExpression><![CDATA[$F{city}]]></textFieldExpression> </textField> </jr:listContents> </jr:list> </componentElement> </detail> </jasperReport>
This approach will generate a report displaying the id field of the JavaBean, as well as the contents of the List elements within a subreport component in the Detail band.
The above is the detailed content of How to Display a JavaBean's List Data in a Jasper Report's Detail Band?. For more information, please follow other related articles on the PHP Chinese website!