【发布时间】:2016-12-01 04:20:38
【问题描述】:
文档已经过时,无论如何都没有帮助。我使用对话框添加类和静态方法,以及保存相关类的 .jar 文件的路径。
当我点击测试连接时,我收到一个错误,说它找不到类 ....
是的,jar 文件位于该路径。我是否需要在项目属性的其他地方或其他地方进一步走这条路?
这是应该描述此过程的文档部分的link
【问题讨论】:
标签: jasper-reports jaspersoft-studio
文档已经过时,无论如何都没有帮助。我使用对话框添加类和静态方法,以及保存相关类的 .jar 文件的路径。
当我点击测试连接时,我收到一个错误,说它找不到类 ....
是的,jar 文件位于该路径。我是否需要在项目属性的其他地方或其他地方进一步走这条路?
这是应该描述此过程的文档部分的link
【问题讨论】:
标签: jasper-reports jaspersoft-studio
我认为你的问题是类的全名 - 你的情况可能缺少包。
这是它在 Jaspersoft Studio 6.2.1 (JSS) 中的工作示例。
豆子顺序:
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;
}
}
豆类产品:
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 对象的 Collection 的工厂:
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;
}
}
所有类都在 ru.alex 包中。
JSS中JavaBeans集合类型数据适配器的设置:
此数据适配器是在向导的帮助下创建的:
我没有在 JSS 中将 beans.jar 添加到项目的 Java Build Path 中,并且一切(适配器)工作正常。可以通过按Test按钮来检查。
复选框使用字段描述在这个游戏中没有任何作用。
我在设置中使用了完整的类名:ru.alex.OrderFactory。
现在这个适配器可以在报告中使用了。
既然适配器已经准备好了,我们就可以使用它了。
在Dataset and Query Dailog,我们可以忽略class not found by ....的消息,并在设置类名后手动添加字段。
报告将是这样的:
<jasperReport ...>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBeanCollection - orders"/>
<field name="price" class="java.lang.Double"/>
如果我们将带有 bean 的 jar 添加到 IDE 构建路径,如下所示:
行为将被改变。在Dataset and Query Dailog输入类名后,字段列表会自动出现:
添加第二个 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? 帖子中描述的问题。我们应该只保留一个 jar 和 Bean 类。例如,jar 位于 Java 构建路径。
完整的描述在参考的帖子中。
【讨论】: