Jaspersoft Studio:如何在数据适配器中使用Java Bean集合

use*_*001 4 jasper-reports jaspersoft-studio

文档已过时,仍然无济于事。我使用对话框添加类和静态方法,以及包含相关类的.jar文件的路径。

当我点击测试连接时,我收到一条错误消息,提示它找不到课程。

在此处输入图片说明

是的,jar文件位于该路径。我是否需要在项目属性中其他地方的那条路径上走得更远?

这是指向该过程的文档部分的链接

Ale*_*x K 6

我认为您的课程全名是您的问题-您的情况下可能缺少该包装。

样品

这是在Jaspersoft Studio 6.2.1JSS)中如何工作的示例。

Java代码

豆类订单:

package ru.alex;

public class Order {

    private double price;
    private int quantity;
    private Product product;

    public double getPrice() {
        return this.price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQuantity() {
        return this.quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public Product getProduct() {
        return this.product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public Order(double price, int quantity, Product product) {
        this.price = price;
        this.quantity = quantity;
        this.product = product;
    }
}
Run Code Online (Sandbox Code Playgroud)

产品

package ru.alex;

public class Product {

    private String name;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Product(String name) {
        this.name = name;
    }
}
Run Code Online (Sandbox Code Playgroud)

和工厂越来越收集订单对象与静态方法:

package ru.alex;

import java.util.*;

public class OrderFactory {

    public static Collection<Order> getOrders() {
        List<Order> orders = new ArrayList<>();
        orders.add(new Order(8.0, 2, new Product("apples")));
        orders.add(new Order(2.5, 10, new Product("oranges")));
        return orders;
    }
}
Run Code Online (Sandbox Code Playgroud)

所有类都在ru.alex包中。

数据适配器设置

JSS 中JavaBeans类型数据适配器的Collection的设置:

适配器设定

此数据适配器是在向导的帮助下创建的:

适配器类型

我没有在JSS中将beans.jar添加到项目的Java Build Path中,并且一切(适配器)都可以正常工作。可以通过按测试按钮进行检查。

复选框“ 使用”字段描述在此游戏中不起作用。

ru.alex.OrderFactory在设置中使用了完整的类名:

现在,该适配器可以在报表中使用。

创建报告的模板

由于适配器已准备就绪,我们可以使用它。

数据集和查询Dailog中,我们可以忽略该消息,class not found by ....并在设置类名称后手动添加字段。

数据集和查询对话框

该报告将是这样的:

<jasperReport ...>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBeanCollection - orders"/>
    <field name="price" class="java.lang.Double"/>
Run Code Online (Sandbox Code Playgroud)

如果将带有我们的bean的罐子添加到IDE构建路径,如下所示:

库路径

行为将会改变。在数据集和查询Dailog中输入类名称后,字段列表将自动出现:

数据集和查询Dailog,自动填充

添加第二个jar后,我们会遇到ClassCastException麻烦。仅将具有相同类的单个jar添加到类路径(JSS)。请查看帖子的底部以查找更多信息。

范本

如果只想显示Order类中的字段,则只能使用数据集和查询Dailog来构造字段列表。

用于显示订单价格和数量的jrxml将是:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Report with Bean" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBeanCollection - orders"/>
    <field name="product" class="ru.alex.Product">
        <fieldDescription><![CDATA[product]]></fieldDescription>
    </field>
    <field name="quantity" class="java.lang.Integer">
        <fieldDescription><![CDATA[quantity]]></fieldDescription>
    </field>
    <field name="price" class="java.lang.Double">
        <fieldDescription><![CDATA[price]]></fieldDescription>
    </field>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="10" y="0" width="100" height="30"/>
                <textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="110" y="0" width="100" height="30"/>
                <textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>
Run Code Online (Sandbox Code Playgroud)

例如,如果要显示产品名称,则需要添加新字段:

<field name="productName" class="java.lang.String">
    <fieldDescription><![CDATA[product.name]]></fieldDescription>
</field>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,模板为:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Report with Bean" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBeanCollection - orders"/>
    <field name="product" class="ru.alex.Product">
        <fieldDescription><![CDATA[product]]></fieldDescription>
    </field>
    <field name="quantity" class="java.lang.Integer">
        <fieldDescription><![CDATA[quantity]]></fieldDescription>
    </field>
    <field name="price" class="java.lang.Double">
        <fieldDescription><![CDATA[price]]></fieldDescription>
    </field>
    <field name="productName" class="java.lang.String">
        <fieldDescription><![CDATA[product.name]]></fieldDescription>
    </field>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="10" y="0" width="100" height="30"/>
                <textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="110" y="0" width="100" height="30"/>
                <textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="210" y="0" width="100" height="30"/>
                <textFieldExpression><![CDATA[$F{productName}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>
Run Code Online (Sandbox Code Playgroud)

谨防!我们可以面对在尝试从数据适配器检索bean时为什么会出错的问题中描述的问题发布。我们应该只停留一个罐子类。例如,在Java Build Path的jar

完整的描述在参考职位。