0
votes

I have some problem with generating a subreport in JasperReports. I have 2 reports which I made in iReport.

Main report:

<?xml version="1.0" encoding="UTF-8"?>
<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="raporcik" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="autor" class="java.lang.String"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["C:\\Documents and Settings\\user\\workspace\\Jasper\\report\\"]]></defaultValueExpression>
</parameter>
<field name="name" class="java.lang.String"/>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="79" splitType="Stretch">
        <staticText>
            <reportElement x="242" y="31" width="115" height="28"/>
            <textElement>
                <font size="18"/>
            </textElement>
            <text><![CDATA[Something]]></text>
        </staticText>
        <textField>
            <reportElement x="439" y="59" width="100" height="20"/>
            <textElement/>
            <textFieldExpression class="java.lang.String"><![CDATA[$P{autor}]]></textFieldExpression>
        </textField>
    </band>
</title>
<pageHeader>
    <band height="35" splitType="Stretch"/>
</pageHeader>
<detail>
    <band height="28" splitType="Stretch"/>
    <band height="50">
        <subreport>
            <reportElement x="46" y="0" width="200" height="50"/>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            <subreportExpression class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + "info.jasper"]]></subreportExpression>
        </subreport>
    </band>
</detail>
<summary>
    <band height="42" splitType="Stretch"/>
</summary>

Second Report(subreport):

<?xml version="1.0" encoding="UTF-8"?>
<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="info" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<field name="name" class="java.lang.String"/>
<background>
    <band splitType="Stretch"/>
</background>
<detail>
    <band height="52" splitType="Stretch">
        <textField>
            <reportElement x="37" y="12" width="100" height="20"/>
            <textElement/>
            <textFieldExpression class="java.lang.String"><![CDATA[$F{name}]]></textFieldExpression>
        </textField>
    </band>
</detail>
</jasperReport>

And Java Code:

Code in Main method:

    JasperDesign jsp = JRXmlLoader.load("C:/Documents and Settings/user/workspace/Jasper/report/raporcik.jrxml");       
    JasperCompileManager.compileReportToFile("C:/Documents and Settings/user/workspace/Jasper/report/info.jrxml");      
    JasperReport jasperReport = JasperCompileManager.compileReport(jsp);        

    Map<String, Object> parametros = new HashMap<String, Object>();
    parametros.put("autor", "Jasper W.");
    parametros.put("SUBREPORT_DIR","C:/Documents and Settings/user/workspace/Jasper/report/");
    List lista = new ArrayList();
    lista.add(new Name("something1"));
    lista.add(new Name("something2"));

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parametros, new JRBeanCollectionDataSource(lista));    


    JRExporter exporter = new JRPdfExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRExporterParameter.OUTPUT_FILE, new java.io.File("C:/Documents and Settings/user/workspace/Jasper/report/hello.pdf"));
    exporter.exportReport();    

And Name Class:

 public class Name {

String name;

public Name(){

}

public Name(String _name){
    this.name = _name;      
}   

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

I don't know what I am doing wrong, but I can't generate subreport. It is always empty. I have the same problem when subreport contains only static text.

2

2 Answers

0
votes

I tried for a few days to get sub-reports to work, and even though I followed the doc exactly, it didn't work.

IMHO "sub-reports" don't actually work.

0
votes

Your subreport is empty probably because you have no datasource or query in your subreports's or main report's JRXML. You can find more information in this answer. It has exactly what you need to use your subreport with only static fields.

So your problem is not exactly with subreports, but with the way the Jasper Reports environment generates reports.