例如,您有一对JavaBean(POJO):
public class Order {
private double price;
private int quantity;
private Product product;
// public getters
}
public class Product {
private String name;
// public getters
}
并且您以这种方式声明报告的数据源:(是的,我喜欢 Guava )
JRBeanCollectionDataSource datasource = new JRBeanCollectionDataSource(Lists.newArrayList(ImmutableList.<Order>builder()
.add(new Order(1000.2, 10, new Product("Phone")))
.add(new Order(10200.0, 2, new Product("Tv")))
.build()));
如果使用此字段声明:
<field name="order" class="java.lang.Object">
<fieldDescription><![CDATA[_THIS]]></fieldDescription>
</field>
<field name="price" class="java.lang.Double"/>
<field name="quantity" class="java.lang.Integer"/>
<field name="productName" class="java.lang.String">
<fieldDescription><![CDATA[product.name]]></fieldDescription>
</field>
您可以使用以下表达式:
<textField>
<reportElement x="0" y="0" width="100" height="30"/>
<textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="30"/>
<textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="0" width="100" height="30"/>
<textFieldExpression><![CDATA[$F{productName}]]></textFieldExpression>
</textField>