简体   繁体   English

JavaBean DataSource不会传递给子子报告

[英]JavaBean DataSource doesn't get passed to the sub-sub-report

I'm having trouble finding out how to correctly pass a JavaBean DS to a subsubreport. 我无法找到如何正确地将JavaBean DS传递给子子报告。 I have the following Java code: 我有以下Java代码:

     JRDataSource javaBeansKapitelDS = new JRBeanCollectionDataSource(BeanFactory.generateKapitelCollection());
     jasperReport = JasperCompileManager.compileReport("JRXML/Subreports.jrxml");
     jasperUnterkapitelReport = JasperCompileManager.compileReport("JRXML/Subreports_subreport1.jrxml");
     jasperEntryReport = JasperCompileManager.compileReport("JRXML/Subreports_subreport1_subreport1.jrxml");

     params.put("SUB_DATASOURCE", BeanFactory.generateUnterKapitelCollection());
     params.put("SUB_SUB_DATASOURCE", BeanFactory.generateEntryCollection());

     jasperPrint = JasperFillManager.fillReport(jasperReport, params, javaBeansKapitelDS);
     JasperExportManager.exportReportToPdfFile(jasperPrint, "output/TestJAVABeansDS.pdf");

In the main report i have a report, which has a subreport, which in turn has its own subreport. 在主报告中,我有一个报告,它有一个子报告,而子报告又有自己的子报告。 In the main report i set the Datasource of the subreport as DataSource Expression new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{SUB_DATASOURCE}) and works just fine! 在主报告中,我将子报表的数据源设置为DataSource Expression new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{SUB_DATASOURCE})并且工作正常!

In the subreport i tried doing the same thing for the subsubreport ( new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{SUB_SUB_DATASOURCE}) ) but i can't pass the SUB_SUB_DATASOURCE parameter from the main report to the subreport in order to use it there. 在子报表中,我尝试对subsubreport执行相同的操作( new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{SUB_SUB_DATASOURCE}) )但我无法将SUB_SUB_DATASOURCE参数从主报表传递到子报表中为了在那里使用它。 If i define in the main report a parameter for the subreport: 如果我在主报表中定义子报表的参数:

<subreportParameter name="SUB_SUB_DATASOURCE"> <subreportParameterExpression><![CDATA[$P{SUB_SUB_DATASOURCE}]]></subreportParameterExpression>

i get an exception 我得到一个例外

Caused by: java.lang.NoSuchMethodException: Unknown property '' on class 'class jasperreports.datasource.Entry'

...

Fill 1: exception

net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error retrieving field value from bean :

... 

I'm using the latest JasperReports library 5.5.1 我正在使用最新的JasperReports库5.5.1

So my question is: how do i pass to the subreport the JavaBeansDS in order to use it there for the subsubreport? 所以我的问题是:我如何将子报告传递给子报告以便在子报告中使用它?

The solution is pretty simple. 解决方案非常简单。 You are already passing the parameters to the subreport, you just need to do the same again within the subreport to pass it to the subsubreport. 您已经将参数传递给子报表,您只需要在子报表中再次执行相同的操作即可将其传递给子报表。 Lets call your reports A, B and C. A is the main report, which contains B, which contains C. 让我们调用您的报告A,B和C. A是主报告,其中包含B,其中包含C.

Report A contains the following parameters (which you set from Java by calling params.put ): 报告A包含以下参数(您通过调用params.put从Java设置):

<parameter name="SUB_DATASOURCE" class="java.util.Collection" />
<parameter name="SUB_SUB_DATASOURCE" class="java.util.Collection" />

Report A also contains the first subreport component: 报告A还包含第一subreport 报告组件:

<subreport>
    ...
    <subreportParameter name="SUB_DATASOURCE">
        <subreportParameterExpression><![CDATA[$P{SUB_DATASOURCE}]]></subreportParameterExpression>
    </subreportParameter>
    <subreportParameter name="SUB_SUB_DATASOURCE">
        <subreportParameterExpression><![CDATA[$P{SUB_SUB_DATASOURCE}]]></subreportParameterExpression>
    </subreportParameter>
    <dataSourceExpression><![CDATA new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{SUB_DATASOURCE})]]></dataSourceExpression>
    ...
</subreport>

Report B contains the same parameters, passed down from report A: 报告B包含从报告A传递的相同参数:

<parameter name="SUB_DATASOURCE" class="java.util.Collection" />
<parameter name="SUB_SUB_DATASOURCE" class="java.util.Collection" />

You now need to pass SUB_SUB_DATASOURCE to report C by including it as a subreportParameter again. 您现在需要再次将SUB_SUB_DATASOURCE作为subreportParameter传递给报表C. So report B will contain the second subreport component as follows: 因此报告B将包含第二个子报告组件,如下所示:

<subreport>
    ...
    <subreportParameter name="SUB_SUB_DATASOURCE">
        <subreportParameterExpression><![CDATA[$P{SUB_SUB_DATASOURCE}]]></subreportParameterExpression>
    </subreportParameter>
    <dataSourceExpression><![CDATA new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{SUB_SUB_DATASOURCE})]]></dataSourceExpression>
    ...
</subreport>

If you want to use the parameter in Report C , you can then include it as follows: 如果要在报表C中使用该参数,则可以按如下方式包含该参数:

<parameter name="SUB_SUB_DATASOURCE" class="java.util.Collection" />

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

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