Home  >  Article  >  Java  >  How to Display Data from a Java List in a JavaBean Using JRBeanCollectionDataSource?

How to Display Data from a Java List in a JavaBean Using JRBeanCollectionDataSource?

DDD
DDDOriginal
2024-11-26 15:50:10909browse

How to Display Data from a Java List in a JavaBean Using JRBeanCollectionDataSource?

JRBeanCollectionDataSource: Displaying Data from a Java List in a JavaBean

Overview

JavaBean-based reports often involve displaying data contained within Java Lists. This article explores how to achieve this using JRBeanCollectionDataSource.

Implementation

Step 1: Create a JavaBean with a List Field

public class Userinfo {
    private String username;
    private String password;
    private List<Address> listAddress;
}

Step 2: Generate Dataset for the List

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);
}

Step 3: Define the JRXML Report

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>

Key Points

  • _THIS expression is used in the subdataset to retrieve the current element of the list.
  • The jr:list component within the Detail band displays the list's elements.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn