[英]How to get the value of complex JavaBean
我有一個.jrxml文件,我想從代碼中傳遞一些參數。 我有一個Orde
r類,其中包含double price
, int quantity
和Product product
等字段。 情況很簡單,當我需要通過價格或數量時,我只是這樣做:
<textFieldExpression class = "java.lang.Integer">
<![CDATA[$F{quantity}]]>
</textFieldExpression>
當我嘗試傳遞product.getName()
時出現問題。 我嘗試過類似的東西:
<textFieldExpression class = "java.lang.String">
<![CDATA[$F{product}.getName()]]>
</textFieldExpression>
還有很多其他但我不斷收到錯誤: net.sf.jasperreports.engine.design.JRValidationException: Report design not valid : 1. Field not found : product
你知道如何解決這個問題嗎?
例如,您有一對JavaBeans(POJO):
public class Order {
private double price;
private int quantity;
private Product product;
// public getters
}
public class Product {
private String name;
// public getters
}
你以這樣的方式聲明報告的數據源:( 是的,我喜歡番石榴 )
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>
注意:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.