动态报告:从bean检索字段值时出错

时间:2015-01-24 09:10:57

标签: java collections datasource dynamic-reports

我正在使用Dynamic 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);

这是我的班级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

1 个答案:

答案 0 :(得分:0)

JRBeanCollectionDataSource需要一个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;
    }