Jaspersoft Studio:如何在数据适配器中使用Java Bean的集合 [英] Jaspersoft Studio: How to use Collection of Java Beans in data adapter

查看:213
本文介绍了Jaspersoft Studio:如何在数据适配器中使用Java Bean的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档过时,无论如何都没有帮助。我使用对话框添加类和静态方法,以及保存相关类的.jar文件的路径。



当我点击测试连接时得到一个错误,说它无法找到班级....





是的,jar文件就在那条路径上。我是否需要在项目属性中的其他位置进一步处理该路径?



这是一个



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





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



复选框使用字段描述不起任何作用这个游戏。



我在设置中使用了完整的班级名称: ru.alex.OrderFactory



现在这个适配器可用于报告。



创建报告模板



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



数据集和查询Dailog ,我们可以忽略<$ c $的消息c> ...... 找不到类,并在设置类名后手动添加字段。





报告将是这样的:

 < jasperReport ...> 
< property name =com.jaspersoft.studio.data.defaultdataadaptervalue =JavaBeanCollection - orders/>
< field name =priceclass =java.lang.Double/>

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





行为将被更改。在数据集和查询Dailog 中键入类名后,将自动显示字段列表:





添加第二个jar后,我们可以解决 ClassCastException 问题。只应将具有相同类的单个 jar 添加到类路径( JSS )中。请查看帖子底部以查找更多信息。



模板



如果我们想要只显示来自 Order 类的字段,我们只能使用数据集和查询Dailog 来构建字段列表。



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

 <?xml version =1.0encoding =UTF-8?> 
< jasperReport xmlns =http://jasperreports.sourceforge.net/jasperreportsxmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http: //jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsdname =用Bean报告pageWidth =595pageHeight =842columnWidth =555leftMargin = 20rightMargin =20topMargin =20bottomMargin =20>
< property name =com.jaspersoft.studio.data.defaultdataadaptervalue =JavaBeanCollection - orders/>
< field name =productclass =ru.alex.Product>
< fieldDescription><![CDATA [product]]>< / fieldDescription>
< / field>
< field name =quantityclass =java.lang.Integer>
< fieldDescription><![CDATA [数量]]>< / fieldDescription>
< / field>
< field name =priceclass =java.lang.Double>
< fieldDescription><![CDATA [price]]>< / fieldDescription>
< / field>
< detail>
< band height =30splitType =Stretch>
< textField>
< reportElement x =10y =0width =100height =30/>
< textFieldExpression><![CDATA [$ F {quantity}]]>< / textFieldExpression>
< / textField>
< textField>
< reportElement x =110y =0width =100height =30/>
< textFieldExpression><![CDATA [$ F {price}]]>< / textFieldExpression>
< / textField>
< / band>
< / detail>
< / jasperReport>

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

 < field name =productNameclass =java.lang.String> 
< fieldDescription><![CDATA [product.name]]>< / fieldDescription>
< / field>

在这种情况下,模板为:

 <?xml version =1.0encoding =UTF-8?> 
< jasperReport xmlns =http://jasperreports.sourceforge.net/jasperreportsxmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http: //jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsdname =用Bean报告pageWidth =595pageHeight =842columnWidth =555leftMargin = 20rightMargin =20topMargin =20bottomMargin =20>
< property name =com.jaspersoft.studio.data.defaultdataadaptervalue =JavaBeanCollection - orders/>
< field name =productclass =ru.alex.Product>
< fieldDescription><![CDATA [product]]>< / fieldDescription>
< / field>
< field name =quantityclass =java.lang.Integer>
< fieldDescription><![CDATA [数量]]>< / fieldDescription>
< / field>
< field name =priceclass =java.lang.Double>
< fieldDescription><![CDATA [price]]>< / fieldDescription>
< / field>
< field name =productNameclass =java.lang.String>
< fieldDescription><![CDATA [product.name]]>< / fieldDescription>
< / field>
< detail>
< band height =30splitType =Stretch>
< textField>
< reportElement x =10y =0width =100height =30/>
< textFieldExpression><![CDATA [$ F {quantity}]]>< / textFieldExpression>
< / textField>
< textField>
< reportElement x =110y =0width =100height =30/>
< textFieldExpression><![CDATA [$ F {price}]]>< / textFieldExpression>
< / textField>
< textField>
< reportElement x =210y =0width =100height =30/>
< textFieldExpression><![CDATA [$ F {productName}]]>< / textFieldExpression>
< / textField>
< / band>
< / detail>
< / jasperReport>






小心!我们可能会遇到为什么我在尝试从数据适配器中检索bean时出错?的问题。我们应该只使用 Bean 类保留一个 jar 。例如, Java Build Path 中的 jar



完整的解释是参考帖子。


Docs are outdated and not helpful anyway. I use the dialog to add the class and and the static method, as well as the path to the .jar file holding the relevant classes.

When I hit test connection I get an error saying it cannot find the class ....

Yes the jar file is at that path. Do I need to further at that path somewhere else in the project properties or something??

Here's a link to the part of the docs that is supposed to describe this process

解决方案

I think your problem in full name of class - probably the package is missing in your case.

Sample

Here is the sample how it works in Jaspersoft Studio 6.2.1 (JSS).

Java code

Bean Order:

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

Bean Product:

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

And the factory for getting Collection of Order objects with static method:

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

All classes are at ru.alex package.

Data adapter settings

The settings of Collection of JavaBeans type data adapter in JSS:

This data adapter was created with help of wizard:

I did not add the beans.jar to the project's Java Build Path in JSS and everything (adapter) works ok. It can be checked by pressing of Test button.

The checkbox Use field description does not play any role in this game.

I used the full class name: ru.alex.OrderFactory in settings.

And now this adapter can be used in report.

Creating report's template

Since the adapter is ready we can use it.

At Dataset and Query Dailog we can ignore message that class not found by .... and manually add the fields after setting the class name.

The report will be something like this:

<jasperReport ...>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBeanCollection - orders"/>
    <field name="price" class="java.lang.Double"/>

If we add the jar with our beans to the IDE build path, like this:

the behaviour will be changed. The list of fields will be appear automatically after typing the class name at Dataset and Query Dailog:

After adding second jar we can got the ClassCastException trouble. Only single jar with same classes should be added to classpath (JSS). Please look at the bottom of a post to find more information.

Templates

If we want to show only field from Order class we can use only the Dataset and Query Dailog to construct fields list.

The jrxml for showing prices & quantities of orders will be:

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

If we want to show, for example the product name we need to add the new field:

<field name="productName" class="java.lang.String">
    <fieldDescription><![CDATA[product.name]]></fieldDescription>
</field>

In this case the template be:

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


Beware! We can face the issue described at Why do I get error when trying to retrive bean from my data adapter? post. We should stay only one jar with Bean classes. For example, jar at Java Build Path.

The full desciption is at referenced post.

这篇关于Jaspersoft Studio:如何在数据适配器中使用Java Bean的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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