JavaBean-based reports often involve displaying data contained within Java Lists. This article explores how to achieve this using JRBeanCollectionDataSource.
public class Userinfo { private String username; private String password; private List<Address> listAddress; }
private static JRDataSource getDataSource() { Collection<BeanWithList> coll = new ArrayList<BeanWithList>(); coll.add(new BeanWithList(Arrays.asList("London", "Paris"), 1)); coll.add(new BeanWithList(Arrays.asList("London", "Madrid", "Moscow"), 2)); coll.add(new BeanWithList(Arrays.asList("Rome"), 3)); return new JRBeanCollectionDataSource(coll); }
In the report, create a subdataset for the list and a componentElement (jr:list) in the Detail band.
<subDataset name="dataset1"> <field name="city" class="java.lang.String"> <fieldDescription><![CDATA[_THIS]]></fieldDescription> </field> </subDataset> ... <detail> <componentElement> <jr:list printOrder="Vertical"> <datasetRun subDataset="dataset1"> <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{cities})]]></dataSourceExpression> </datasetRun> </jr:list> </componentElement> </detail>
The above is the detailed content of How to Display Data from a Java List in a JavaBean Using JRBeanCollectionDataSource?. For more information, please follow other related articles on the PHP Chinese website!