Home > Java > javaTutorial > How to Display a JavaBean\'s List in JasperReports\' Detail Band?

How to Display a JavaBean\'s List in JasperReports\' Detail Band?

Linda Hamilton
Release: 2024-11-25 22:11:12
Original
145 people have browsed it

How to Display a JavaBean's List in JasperReports' Detail Band?

JavaBeans Containing java.util.List: Display in JasperReports

Problem

A JavaBean contains a java.util.List. How can this list's data be displayed in the Detail band of a JasperReports document?

Solution

  1. Use _THIS Expression: Utilize the _THIS expression to access the JavaBean's members.
  2. Apply jr:list Component: Incorporate a jr:list component in the Detail band to iterate through the list's items.

Working Sample Code

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

Results

This configuration generates a report displaying the list's data within the Detail band.

Additional Information

  • Other relevant questions address this topic:

    • "How do I print a list of strings contained within another list in iReport?"
    • "Passing the List of primitive type objects as datasource for subreport"

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!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template