Home  >  Article  >  Java  >  How to Display a JavaBean\'s List Data in a Jasper Report\'s Detail Band?

How to Display a JavaBean\'s List Data in a Jasper Report\'s Detail Band?

Barbara Streisand
Barbara StreisandOriginal
2024-11-23 12:30:14121browse

How to Display a JavaBean's List Data in a Jasper Report's Detail Band?

JRBeanCollectionDataSource: Displaying Data from a java.util.List in JavaBeans

Introduction

JavaBeans often contain collections of data. JRBeanCollectionDataSource allows you to display data from these collections in a variety of ways.

Question

How can I display data from a java.util.List stored in a JavaBean in the Detail band of a Jasper report?

Answer

Key Considerations for Solution:

  • Use the _THIS expression to access the JavaBean object.
  • Utilize the List (jr:list) component within the Detail band.

Implementation

Java Class:

// JavaBean with a list of strings
public class BeanWithList {
  private List<String> cities;
  private Integer id;

  // Getter methods
}

JRXML:

<jasperReport>
  <subDataset name="dataset1">
    <field name="city" class="java.lang.String">
      <fieldDescription><![CDATA[_THIS]]></fieldDescription>
    </field>
  </subDataset>
  <field name="id" class="java.lang.Integer"/>
  <field name="cities" class="java.util.Collection"/>

  <title>
    <staticText>Bean with List sample</staticText>
  </title>

  <detail>
    <textField>
      <reportElement x="0" y="0" width="100" height="20"/>
      <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
    </textField>
    <componentElement>
      <reportElement x="100" y="0" width="400" height="20"/>
      <jr:list>
        <datasetRun subDataset="dataset1">
          <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{cities})]]></dataSourceExpression>
        </datasetRun>
        <jr:listContents height="20" width="400">
          <textField>
            <reportElement x="0" y="0" width="100" height="20"/>
            <textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
          </textField>
        </jr:listContents>
      </jr:list>
    </componentElement>
  </detail>
</jasperReport>

Result

This approach will generate a report displaying the id field of the JavaBean, as well as the contents of the List elements within a subreport component in the Detail band.

The above is the detailed content of How to Display a JavaBean\'s List Data in a Jasper Report\'s Detail Band?. 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