gpt4 book ai didi

jasper-reports - Jaspersoft Studio : How to use Collection of Java Beans in data adapter

转载 作者:行者123 更新时间:2023-12-04 14:55:54 27 4
gpt4 key购买 nike

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

当我点击测试连接时,我收到一条错误消息,说它找不到类....

enter image description here

是的,jar 文件在那个路径上。我是否需要在项目属性中的其他地方进一步走这条路?

这是一个 link到应该描述此过程的文档部分

最佳答案

我认为您的问题是类(class)的全名 - 您的情况可能缺少包。
样本
以下是它在 Jaspersoft Studio 6.2.1 (JSS) 中如何工作的示例。
Java代码
bean 顺序:

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 制品:
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;
}
}
以及使用静态方法获取 Order 对象集合的工厂:
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;
}
}
所有类(class)都在 ru.alex 包中。
数据适配器设置
JSS中JavaBeans类型数据适配器的集合设置:
Adapter Settings
这个数据适配器是在向导的帮助下创建的:
Type of adapter
我没有在 JSS 中将 beans.jar 添加到项目的 Java Build Path 中,一切(适配器)都可以正常工作。可以通过按“测试”按钮进行检查。
复选框使用字段描述在此游戏中没有任何作用。
我使用了完整的类(class)名称: ru.alex.OrderFactory在设置中。
现在这个适配器可以在报告中使用。
创建报告模板
由于适配器已准备就绪,我们可以使用它。
在 Dataset 和 Query Dailog 我们可以忽略消息 class not found by ....并在设置类名后手动添加字段。
Dataset and Quqery dialog
报告将是这样的:
<jasperReport ...>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBeanCollection - orders"/>
<field name="price" class="java.lang.Double"/>
如果我们将带有 bean 的 jar 添加到 IDE 构建路径,如下所示:
The libs path
行为将被改变。在 Dataset 和 Query Dailog 中输入类名后,字段列表将自动出现:
Dataset and Query Dailog, auto filling
添加第二个 jar 后,我们会遇到 ClassCastException 问题。只应将具有相同类的单个 jar 添加到类路径 (JSS)。请查看帖子底部以查找更多信息。
模板
如果我们只想显示 Order 类中的字段,我们只能使用 Dataset 和 Query 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>
如果我们想显示,例如我们需要添加新字段的产品名称:
<field name="productName" class="java.lang.String">
<fieldDescription><![CDATA[product.name]]></fieldDescription>
</field>
在这种情况下,模板是:
<?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>

谨防! 我们可以面对 Why do I get error when trying to retrive bean from my data adapter? 中描述的问题邮政。我们应该只保留一个带有 Bean 类的 jar。例如,Java 构建路径中的 jar。
完整的描述在引用的帖子中。

关于jasper-reports - Jaspersoft Studio : How to use Collection of Java Beans in data adapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40902602/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com