Home > Java > javaTutorial > 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
Release: 2024-11-26 15:50:10
Original
964 people have browsed it

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;
}
Copy after login

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);
}
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template