2016-08-08 1 views
5

私は.jrxmlファイルを持っています。コードからいくつかのパラメータを渡したいと思います。私はdouble priceint quantityProduct productのようなフィールドを持つOrde rクラスを持っています。私は価格や数量を渡す必要があるときの状況は、シンプルですが、私はちょうどこのような何か:私はproduct.getName()を渡すしようとすると、複合JavaBeanの値を取得する方法

<textFieldExpression class = "java.lang.Integer"> 
    <![CDATA[$F{quantity}]]> 
</textFieldExpression> 

問題が表示されます。私のような何か試してみました:

<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

あなたがこの問題を解決するためにどのように任意のアイデアを持っていますか?例えば

+0

参照:http://stackoverflow.com/a/38535589/59087 –

答えて

2

あなたは、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> 

注:JavaBean Data Sources

  • 関連する問題