I have been trying to load subreport by passing the subreport url to the jasperreportpdf view object. But can't successfully do it. I tried this code but have this error

net.sf.jasperreports.engine.JRException: Resource not found at: src/main/resources/reports/subdeptSubreport.jasper.

My code:

@GetMapping("sub-report.html")
public ModelAndView showSubreport() throws IOException, JRException {
        ModelAndView mav;
        JasperReportsPdfView view = new JasperReportsPdfView();
        view.setUrl("classpath:/reports/subreport_A4.jrxml");
        view.setApplicationContext(context);
        List<Department> deptList = departmentService.findAllDepartment();
        List<SubDepartment> subDepartmentList = subDepartmentService.readAllSubDept();
        mav = new ModelAndView(view);
        view.setReportDataKey("dept");
        Properties prop = new Properties();
        URL in = this.getClass().getResource("/reports/subdeptSubreport.jrxml");

        prop.load(in.openStream());
        view.setSubReportUrls(prop);
        mav.addObject("dept",deptList);
        view.setSubReportDataKeys("subdept");
        mav.addObject("subdept", subDepartmentList);
        return mav;
}

This is my 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="subdeptSubreport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9af1cb75-3bcb-4215-951c-b1da7c0b7552">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="subDepartmentId" class="java.lang.String"/>
    <field name="subDepartmentName" class="java.lang.String"/>
    <columnHeader>
        <band height="61" splitType="Stretch">
            <staticText>
                <reportElement x="30" y="0" width="100" height="30" uuid="58b68441-574c-4ca8-b414-ba66e4f4a973">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="c6824e0a-3bc1-4369-a8ce-3da789563cc9"/>
                </reportElement>
                <text><![CDATA[subDepartmentId]]></text>
            </staticText>
            <staticText>
                <reportElement x="181" y="0" width="100" height="30" uuid="942847a7-e36e-45f1-8549-baa709ebab31">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="09007c5c-d917-415a-952f-80c9c2827be4"/>
                </reportElement>
                <text><![CDATA[subDepartmentName]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="64" splitType="Stretch">
            <textField>
                <reportElement x="30" y="25" width="100" height="30" uuid="bb5efbd4-3f64-4ab9-9ac0-3028eb502e98"/>
                <textFieldExpression><![CDATA[$F{subDepartmentId}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="181" y="25" width="100" height="36" uuid="74c2be9f-ff6f-4e3d-bbf9-d8015636b4d1"/>
                <textFieldExpression><![CDATA[$F{subDepartmentName}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

This is my master 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="subreport_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2440a64e-a164-45c4-b251-927084d800a0">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="departmentID" class="java.lang.Integer"/>
    <field name="departmentName" class="java.lang.String"/>
    <columnHeader>
        <band height="42" splitType="Stretch">
            <staticText>
                <reportElement x="84" y="0" width="100" height="30" uuid="20b54b48-a3a8-475e-a9e5-c3dd3158a4ae"/>
                <text><![CDATA[departmentID]]></text>
            </staticText>
            <staticText>
                <reportElement x="190" y="0" width="100" height="30" uuid="96868f8a-6afa-437f-ae02-382f28ba00df"/>
                <text><![CDATA[departmentName]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="247" splitType="Stretch">
            <subreport>
                <reportElement x="50" y="160" width="200" height="87" uuid="79ef8184-5bc1-4f1d-91fe-5f7480549af5"/>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["src/main/resources/reports/subdeptSubreport.jasper"]]></subreportExpression>
            </subreport>
            <textField>
                <reportElement x="84" y="112" width="100" height="30" uuid="d2ff5964-c842-4fe5-bd08-bcf4cffd952b"/>
                <textFieldExpression><![CDATA[$F{departmentID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="184" y="112" width="100" height="30" uuid="a9b4dff2-707c-456d-a0dd-892f80bdfa45"/>
                <textFieldExpression><![CDATA[$F{departmentName}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

None of the mentioned answers have contain my solution, I here load a master report which contains a subreport. But for loading the subreport It has to be used by using view.setSubReportUrls() method. How to use view.setSubReportUrls(). I have tried using Java,util.properties, but got this mentioned error. Please help me to find the solution.Thanks in advance.

0 Answers