如何获得复杂JavaBean的价值 [英] How to get the value of complex JavaBean

查看:165
本文介绍了如何获得复杂JavaBean的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .jrxml 文件,我想从代码中传递一些参数。我有一个 Orde r类,其字段类似于双倍价格 int quantity 产品。情况很简单,当我需要通过价格或数量时,我只是这样做:

I have a .jrxml file and I would like to pass some params from the code to it. I have an Order class that has fields like double price, int quantity and Product product. The situation is simple, when i need to pass price or quantity, I just do something like this:

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

当我尝试传递 product.getName()时出现问题。我试过类似的东西:

The problem appears when I try to pass product.getName(). I tried something like:

<textFieldExpression class = "java.lang.String">
   <![CDATA[$F{product}.getName()]]>
</textFieldExpression>

以及其他许多人,但我一直收到错误: net.sf.jasperreports。 engine.design.JRValidationException:报表设计无效:1。找不到字段:产品

and many others but I keep getting error: net.sf.jasperreports.engine.design.JRValidationException: Report design not valid : 1. Field not found : product

您是否知道如何解决此问题?

Do you have any idea how to solve this problem?

推荐答案

例如,你有一对JavaBeans(POJO):

For example you have a pair of JavaBeans (POJO):

public class Order {

    private double price;
    private int quantity;
    private Product product;
    // public getters 
}

public class Product {

    private String name;
    // public getters 
}

并以这种方式声明报告的数据源像这样:(是的,我喜欢 Guava

and you declare report's datasource in the manner like this: (yes, I like 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>

你可以使用这样的表达式:

you can use such expressions:

<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>






注意:


Note:


  • 不要忘记吸气剂应该是公开的

  • 更多信息: JavaBean数据源

  • 使用样本的 _THIS 的良好解释可以在这些帖子中找到:

  • Don't forget that getters should be public
  • More info: JavaBean Data Sources
  • The good explanation of _THIS using with samples can be found in this posts:

  1. 如何在jasperreports中访问数据源的根元素

  2. 将基本类型对象列表作为子报表的数据源传递

  3. 如何在iReport中打印另一个列表中包含的字符串列表?

  1. How to access the root element of the datasource in jasperreports
  2. Passing the List of primitive type objects as datasource for subreport
  3. How do I print a list of strings contained within another list in iReport?


这篇关于如何获得复杂JavaBean的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆