gpt4 book ai didi

java - 如何将 JR Bean 数据源仅传递给子报表并将 JREmptyData 源传递给主 IReport

转载 作者:行者123 更新时间:2023-11-29 05:34:38 28 4
gpt4 key购买 nike

我是 jasper 报告的新手,我不知道如何将 JRBeanCollectionDataSource 仅传递给子报表,将 JREmptyDataSource 传递给 master 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

why each element of array list is displaying in new page if bean fields are directly taken in main report

最佳答案

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

Java:

...
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 为空,因为您将空数据源传递给主报表。

关于java - 如何将 JR Bean 数据源仅传递给子报表并将 JREmptyData 源传递给主 IReport,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19946177/

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