简体   繁体   English

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

[英]How to Pass JR Bean Data Source only to subreport and JREmptyData Source to master IReport

I a new to jasper report and i am not getting how to pass JRBeanCollectionDataSource only to subreport and JREmptyDataSource to master ireport.我是 jasper 报告的新手,我不知道如何将 JRBeanCollectionDataSource 仅传递给子报表,将 JREmptyDataSource 传递给主 ireport。

I have one java bean class usingbean having properties compet and compet_name with getter and setter methods.我有一个 java bean 类 usingbean 具有属性 compet 和 compet_name 以及 getter 和 setter 方法。

In my master report i have one field name as dataBeanList which is the Arraylist of type usingbean and field property is set to type list.在我的主报告中,我有一个字段名称为 dataBeanList,它是 usingbean 类型的 Arraylist,并且字段属性设置为类型列表。

My subreport is having two fields named as compet and compet_name...我的子报告有两个字段,分别命名为 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>

My java code is...我的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");
}

My master report is getting filled but subreport is empty with static texts.. Please tell me where i am doing mistakes我的主报告正在填写,但子报告为空,带有静态文本。请告诉我我在哪里做错了

error is:错误是:

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

You need to pass the subreport datasource to the main report as a parameter rather than a field. 您需要将子报表数据源作为参数而不是字段传递给主报表。

Java: 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: 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>
...

In your current code, the field dataBeanList is null as you are passing an empty datasource to the main report. 在当前代码中,当您将空数据源传递给主报表时,字段dataBeanList为null。

List predstave = (List)request.getSession().getAttribute("predstaveR");列表 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM