A JavaBean contains a java.util.List. How can this list's data be displayed in the Detail band of a JasperReports document?
// Java source to generate the report ... // DataSource using JRBeanCollectionDataSource private static JRDataSource getDataSource() { Collection<BeanWithList> coll = ...; return new JRBeanCollectionDataSource(coll); } // JavaBean public class BeanWithList { private List<String> cities; // ... // Ensure a public getter for field extraction public List<String> getCities() { return this.cities; } } // jrxml file ... // Subdataset for iterating through the list <subDataset name="dataset1"> <field name="city" class="java.lang.String"> <fieldDescription><![CDATA[_THIS]]></fieldDescription> </field> </subDataset> // Detail band with jr:list component <detail> ... <componentElement> <!-- jr:list component --> <jr:list ...> <datasetRun subDataset="dataset1"> <!-- DataSource expression using JRBeanCollectionDataSource --> <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{cities})]]></dataSourceExpression> </datasetRun> <jr:listContents ...> <textField ...> <textFieldExpression><![CDATA[$F{city}]]></textFieldExpression> </textField> </jr:listContents> </jr:list> </componentElement> ... </detail> ...
This configuration generates a report displaying the list's data within the Detail band.
Other relevant questions address this topic:
The above is the detailed content of How to Display a JavaBean\'s List in JasperReports\' Detail Band?. For more information, please follow other related articles on the PHP Chinese website!