简体   繁体   中英

How to pass parameters to sub subreports from java code JasperReports

I'm having serious problems trying to figure out how to pass parameters to sub-subreports in jasperreports from java code.

The hierarchy I'm talking about looks like the following:

mainReport

--------------> subReport

---------------------------> subSubReport

So far, I tried to pass the parameters needed in the subSubReport to the mainReport and from there pass it to the subReport and from there to the subSubReport but this it's not working. In fact, I do get a pdf file with the contents of the mainReport and the subReport but the content of the subSubReport is not displaying. That's why I'm guessing my problem is that I shouldn't be passing the parameters (SUBREPORT_DIR more specifically) of the subSubReport to the mainReport but to the subReport and that's what I don't know how to achieve. Also, there is little (and incomplete) information on the web about it.

Can someone please help me?

mainReport code

<subreport>
                <reportElement key="" x="0" y="0" width="554" height="30" isRemoveLineWhenBlank="true" uuid="18ff5513-835d-4910-b39c-6d41252b4679"/>
                <subreportParameter name="REPORT_CONNECTION">
                    <subreportParameterExpression><![CDATA[$P{REPORT_CONNECTION}]]></subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="REPORT_DATA_SOURCE">
                    <subreportParameterExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="SUBREPORT_DIR">
                    <subreportParameterExpression><![CDATA[$P{SUBREPORT_DIR}]]></subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="SUBREPORT1LEVEL1">
                    <subreportParameterExpression><![CDATA[$P{SUBREPORT1LEVEL1}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA[$P{SUBREPORT1LEVEL1}]]></subreportExpression>
</subreport>

subReport code

<subreport>
                <reportElement x="50" y="10" width="457" height="40" uuid="4528914e-bb9a-4300-8173-14be2ff2db85"/>
                <subreportParameter name="REPORT_CONNECTION">
                    <subreportParameterExpression><![CDATA[$P{REPORT_CONNECTION}]]></subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="REPORT_DATA_SOURCE">
                    <subreportParameterExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="SUBREPORT_DIR">
                    <subreportParameterExpression><![CDATA[$P{SUBREPORT_DIR}]]></subreportParameterExpression>
                </subreportParameter>
                <subreportParameter name="SUBREPORT1LEVEL2">
                    <subreportParameterExpression><![CDATA[$P{SUBREPORT1LEVEL2}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA[$P{SUBREPORT1LEVEL2}]]></subreportExpression>
            </subreport>

Currently you are setting each report level with a parameter equalling itself, rather than passing it down.

What you need to do is pass the parameter from the level above for each level of sub-report eg if you have a parameter at the top level, you need to pass this to the sub-level via the associated sub-report parameter, and then pass this level's parameter to the sub-sub-report ie the next level down.

For instance, let's assume we want to pass a Boolean server check parameter from the top level to the bottom, with the parameters "IsOnserver", "IsOnserverL1" and "IsOnserverL2" for each respective report level. At the main report level we would have this:

<parameter name="IsOnServer" class="java.lang.Boolean">
    <defaultValueExpression><![CDATA[true]]></defaultValueExpression>
</parameter>

For the next level (the sub-report) we would do the following, to fill this with the main report parameter's value:

<parameter name="IsOnServerL1" class="java.lang.Boolean">
    <defaultValueExpression><![CDATA[$P{IsOnServer}]]></defaultValueExpression>
</parameter>

For your 3rd and final level parameter (the sub-sub-report) we would pass the sub-report parameter value:

<parameter name="IsOnServerL2" class="java.lang.Boolean">
    <defaultValueExpression><![CDATA[$P{IsOnServerL1}]]</defaultValueExpression>
</parameter>

This will result in the main report parameter being passed down the line correctly.

At the top level you would set and pass the initial parameters, like so:

--set all parameters at top level
<parameter name="SUBREPORT_DIR0" class="java.lang.String">
    <defaultValueExpression><![CDATA["level0Dir"]]></defaultValueExpression>
</parameter>
<parameter name="SUBREPORT_DIR1" class="java.lang.String">
    <defaultValueExpression><![CDATA["level1Dir"]]></defaultValueExpression>
</parameter>
<parameter name="SUBREPORT_DIR2" class="java.lang.String">
    <defaultValueExpression><![CDATA["level2Dir"]]></defaultValueExpression>
</parameter>
--
-- pass parameters to level 1
<subreportParameter name="SUBREPORT_DIR1">
    <subreportParameterExpression><![CDATA[$P{SUBREPORT_DIR1}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="SUBREPORT_DIR2">
    <subreportParameterExpression><![CDATA[$P{SUBREPORT_DIR2}]]></subreportParameterExpression>
</subreportParameter>

For level 1 you would set and pass the next set of parameters:

--set parameters at level 1
<parameter name="SUBREPORT_DIR1" class="java.lang.String">
    <defaultValueExpression><![CDATA[CDATA[$P{SUBREPORT_DIR1}]]></defaultValueExpression>
</parameter>
<parameter name="SUBREPORT_DIR2" class="java.lang.String">
    <defaultValueExpression><![CDATA[CDATA[$P{SUBREPORT_DIR2}]]></defaultValueExpression>
</parameter>

--
-- pass parameter to level 2
<subreportParameter name="SUBREPORT_DIR2">
    <subreportParameterExpression><![CDATA[$P{SUBREPORT_DIR2}]]></subreportParameterExpression>
</subreportParameter>

for the final level you would set the remaining passed parameter:

-- set parameter at level 2
<parameter name="SUBREPORT_DIR2" class="java.lang.String">
    <defaultValueExpression><![CDATA[CDATA[$P{SUBREPORT_DIR2}]]></defaultValueExpression>
</parameter>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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