gpt4 book ai didi

java - 动态报告: Error retrieving field value from bean

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:37 25 4
gpt4 key购买 nike

我正在使用动态 jasper 库为我的报告创建动态列。
这是我的代码:

FastReportBuilder drb = new FastReportBuilder();
DynamicReport dr = drb.addColumn("State", "state", String.class.getName(),30)
.addColumn("Branch", "branch", String.class.getName(),30) // title, property to show, class of the property, width
.addColumn("Product Line", "productLine", String.class.getName(),50)
.addColumn("Item", "item", String.class.getName(),50)
.addColumn("Item Code", "id", Long.class.getName(),20)
.addColumn("Quantity", "quantity", Long.class.getName(),30)
.addColumn("Amount", "amount", Float.class.getName(),30)
.addGroups(2) // Group by the first two columns
.setTitle("November 2006 sales report")
.setSubtitle("This report was generateed at" + new Date())
.setUseFullPageWidth(true) //make colums to fill the page width
.build();

JRDataSource ds = new JRBeanCollectionDataSource(TestRepositoryProducts.getDummyCollection());
JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
JasperViewer.viewReport(jp);

这是我的类(class)TestRepositoryProducts:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Vector;

public class TestRepositoryProducts {

public static Collection<Object> getDummyCollection() {
Collection<Object> v = new ArrayList<Object>();
v.add("qsdqsd");
v.add("qsdqdqs");
v.add("qsdqdqs");
v.add("qsdqdqs");
v.add(32165);
v.add(65456);
v.add(1.5);
return v;
}

}

这是错误:
enter image description here

最佳答案

JRBeanCollectionDataSource 需要一个 Bean 集合,每个 Bean 代表一行,而不是一组对象,每个对象代表一列(正如您所想的那样)。

Bean 类需要为您定义的每个属性提供 getter/setter,例如 getState()、getBranch() 等。

例如:

public class MyBean {
private String state;
// other properties

public MyBean() {}

public String getState() {
return state;
}

public void setState(String s) {
state = s;
}

// other getters and setters
}

您的测试类将创建这些 MyBean 类的集合:

public class TestRepositoryProducts {
public static Collection<MyBean> getDummyCollection() {
Collection<MyBean> v = new ArrayList<MyBean>();
MyBean b1 = new MyBean();
b1.setState("s1");
b1.setBranch("b1");
// set other properties of b1
v.add(b1);

// more beans created and added to v
return v;
}

关于java - 动态报告: Error retrieving field value from bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28123946/

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