One object (java bean) as data source on iReport (

2019-03-11 10:01发布

I'm new in iReport and I have to create a PDF report.

With a JavaBean DataSource you wrap an array (or collection) of JavaBeans but I only need to pass an object (only one instance of a JavaBean). I mean, I have to show in my report the properties of a java bean.

How can I do this? I'm a little bit confused, I have to pass an array with only one item?

3条回答
对你真心纯属浪费
2楼-- · 2019-03-11 10:35

You can pass your bean to the report using a JRBeanArrayDataSource or JRBeanCollectionDataSource or you can use the parameters Map.

JasperPrint reportPrint = JasperFillManager.fillReport(
        this.getClass().getClassLoader().getResourceAsStream("/report.jasper"),
            new HashMap<String,Object>(), 
                 new JRBeanArrayDataSource(new YourBean[]{yourBean}));

or

Map<String,Object> params = new HashMap<String,Object>();
params.put("yourBean", yourBean);

JasperPrint reportPrint = JasperFillManager.fillReport(
        this.getClass().getClassLoader().getResourceAsStream("/report.jasper"),
                params, new JREmptyDataSource());
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-03-11 10:56

Even if you have only one bean, you can still use the collection data source. In this case, the collection will have only 1 object. If you create the report design with correct grouping, this report can later be used to show multiple classes with their respective properties.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-03-11 10:57

If you want to use the pre-defined Jasper Reports classes, then yes. The default implementations of JRAbstractBeanDataSource are limited to dealing with arrays or collections. So you could just create a one element array or one element collection.

The alternative would be to implement your own version of JRAbstractBeanDataSource that stubs out the next() and moveFirst() methods, but that does seem like a silly amount of work to do when there's a straight-forward way to get the required behavior.

查看更多
登录 后发表回答