2

我是 jasper 报告的新手,我不知道如何将 JRBeanCollectionDataSource 仅传递给子报表,将 JREmptyDataSource 传递给主 ireport。

我有一个 java bean 类 usingbean 具有属性 compet 和 compet_name 以及 getter 和 setter 方法。

在我的主报告中,我有一个字段名称为 dataBeanList,它是 usingbean 类型的 Arraylist,并且字段属性设置为类型列表。

我的子报告有两个字段,分别命名为 compet 和 compet_name ...

   <subreport>
                    <reportElement uuid="0db7b8b7-9cf5-41fc-b271-ffb6333c80f3" x="129" y="30" width="200" height="100"/>
                    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{dataBeanList})]]></dataSourceExpression>
                    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "subreport1.jasper"]]></subreportExpression>
                </subreport>

我的java代码是...

   try
        {

             Map<String, Object> parameters = new HashMap<String, Object>();

            newpdftemplate newpdftemplate = new newpdftemplate();
            ArrayList<usingbean> dataBeanList = newpdftemplate.getDataBeanList();

            JRBeanCollectionDataSource beanColDataSource = new
            JRBeanCollectionDataSource(dataBeanList);
jasperDesign = JRXmlLoader.load("D:/workspace/itxtgraph/jrxml/FirstReport.jrxml");
        jasperReport = JasperCompileManager.compileReport(jasperDesign);
parameters.put("SUBREPORT_DIR", "D:/workspace/itxtgraph/jrxml/");
         jasperPrint = JasperFillManager.fillReport(jasperReport,parameters, new JREmptyDataSource());
        JasperExportManager.exportReportToPdfFile(jasperPrint,
        "D:/workspace/itxtgraph/report/SecondReport.pdf");
        response.setContentType("application/pdf");
}

我的主报告正在填写,但子报告为空,带有静态文本。请告诉我我在哪里做错了

错误是:

 13 Nov, 2013 4:44:31 PM net.sf.jasperreports.engine.fill.JRFillSubreport prepare
SEVERE: Fill 1: exception
net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : 
    at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:123)
    at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:96)
    at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:100)
    at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:1331)
    at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1232)
    at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1208)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1577)
    at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:149)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:932)
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:864)
    at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:655)
    at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:59)
    at net.sf.jasperreports.engine.fill.AbstractThreadSubreportRunner.run(AbstractThreadSubreportRunner.java:203)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.NoSuchMethodException: Unknown property '' on class 'class itxtgraph.usingbean'
    at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1313)
    at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:762)
    at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:837)
    at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
    at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)
    ... 15 more

如果直接在主报告中获取bean字段,为什么数组列表的每个元素都显示在新页面中

4

2 回答 2

3

您需要将子报表数据源作为参数而不是字段传递给主报表。

爪哇:

...
Map<String, Object> parameters = new HashMap<String, Object>();
newpdftemplate newpdftemplate = new newpdftemplate();
ArrayList<usingbean> dataBeanList = newpdftemplate.getDataBeanList();
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataBeanList);
parameters.put("subReportDataSource", beanColDataSource);
...

JRXML:

...
<parameter name="subReportDataSource" class="net.sf.jasperreports.engine.JRDataSource"/>
...
<subreport>
    <reportElement uuid="0db7b8b7-9cf5-41fc-b271-ffb6333c80f3" x="129" y="30" width="200" height="100"/>
    <dataSourceExpression><![CDATA[$P{subReportDataSource}]]></dataSourceExpression>
    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "subreport1.jasper"]]></subreportExpression>
</subreport>
...

在您当前的代码中,当您将空数据源传递给主报表时,字段 dataBeanList 为空。

于 2013-11-13T10:04:55.730 回答
0

列表 predstave = (List)request.getSession().getAttribute("predstaveR");

JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(predstave);
InputStream inputStream = this.getClass().getResourceAsStream("/reports/Predstave.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
Map<String, Object> params = new HashMap<String, Object>();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
inputStream.close();

response.setContentType("application/x-download");
response.addHeader("Content-disposition", "attachment; filename=PredstaveRezisera.pdf");
ServletOutputStream out = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint,out);
于 2022-01-21T12:40:43.897 回答