SubReport not shown

2020-07-18 09:40发布

问题:

I'd like to create a simple Jasper Report, with subreport, but I cannot make it work.

For example, I have two, simple report.

test1.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2016-01-18T23:29:14 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="test1" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="553ce57f-5ea7-48e5-8eaa-dcfecf31880a">
    <queryString>
        <![CDATA[select dummy from dual]]>
    </queryString>
    <field name="dummy" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch">
            <staticText>
                <reportElement x="89" y="0" width="100" height="30" uuid="04f9b63d-265b-45f0-a249-41464df6b4c0"/>
                <text><![CDATA[x]]></text>
            </staticText>
            <staticText>
                <reportElement x="227" y="0" width="100" height="30" uuid="0c7db454-653c-4dcf-af94-a0f1c9962b91"/>
                <text><![CDATA[x]]></text>
            </staticText>
            <staticText>
                <reportElement x="352" y="0" width="100" height="30" uuid="7ad861d5-74ba-49e9-95c7-4cd01d184dbe"/>
                <text><![CDATA[x]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="257" splitType="Stretch">
            <textField>
                <reportElement x="89" y="66" width="100" height="30" uuid="4099071d-8bfc-4e61-a697-af078ccb626a"/>
                <textFieldExpression><![CDATA[$F{dummy}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="227" y="68" width="100" height="30" uuid="a955ca4e-aecd-48c3-8b7c-face6295b879"/>
                <textFieldExpression><![CDATA[$F{dummy}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="352" y="65" width="100" height="30" uuid="5a998443-ae4f-457d-a565-67b4f3405951"/>
                <textFieldExpression><![CDATA[$F{dummy}]]></textFieldExpression>
            </textField>
            <subreport>
                <reportElement x="555" y="57" width="200" height="200" uuid="d6eada20-a566-4ba5-ab51-9d6ea0b4dfd9"/>
                <parametersMapExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}]]></parametersMapExpression>
                <subreportParameter name="a">
                    <subreportParameterExpression><![CDATA[12]]></subreportParameterExpression>
                </subreportParameter>
                <subreportExpression><![CDATA["test4.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

test4.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2016-01-18T23:27:56 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="test4" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c55e71d8-6eb9-4f20-9263-c50c295fa0d1">
    <parameter name="a" class="java.lang.String"/>
    <queryString>
        <![CDATA[select dummy from dual where 1=1]]>
    </queryString>
    <field name="dummy" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch">
            <staticText>
                <reportElement x="364" y="0" width="100" height="30" uuid="1d48df0f-3f4c-489f-b479-e981b2727590"/>
                <text><![CDATA[b]]></text>
            </staticText>
            <staticText>
                <reportElement x="478" y="0" width="100" height="30" uuid="397b1c60-fe4d-4f41-a7a4-7c585a57ac15"/>
                <text><![CDATA[a]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch">
            <staticText>
                <reportElement x="239" y="58" width="100" height="30" uuid="5b5c86c0-1b1b-44d3-87cb-2ad2782a3408"/>
                <text><![CDATA[test4]]></text>
            </staticText>
            <textField>
                <reportElement x="364" y="66" width="100" height="30" uuid="d3217648-88a1-4d88-b2b0-7f7cc4e598c6"/>
                <textFieldExpression><![CDATA[$F{dummy}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="478" y="47" width="100" height="30" uuid="f2c6bfb9-ee73-4928-8ce4-0a83060d0801"/>
                <textFieldExpression><![CDATA[$F{dummy}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

The selects provide one result (the DB is Oracle), but the subreport isn't visible in the repport made from test1.jrxml. But a whole new, empty page is at the end of the report.

Can you show me what's wrong with this example? I'd tried a lot's of stuff (default value in the subreport, When No Data = All Sections, No Detail setting in the subReport's header, and a lot of stuff, I can't remember), but nothing helped.

回答1:

In your example I can see two problems:

  1. You need to pass absolute path to subreport (this is normally done by parameter, so that it can be controlled on runtime)
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["C:\\jdd\\projects\\StackTrace\\jasper\\"]]></defaultValueExpression>
</parameter>

<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "test4.jasper"]]></subreportExpression>
  1. You need to pass the report connection to the subreport
<subreport>
    <reportElement x="555" y="57" width="200" height="200" uuid="d6eada20-a566-4ba5-ab51-9d6ea0b4dfd9"/>
    <parametersMapExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}]]></parametersMapExpression>
    <subreportParameter name="a">
        <subreportParameterExpression><![CDATA[12]]></subreportParameterExpression>
    </subreportParameter>
    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "test4.jasper"]]></subreportExpression>
</subreport>