当 Jasper Report 中有子报表时找不到 JRException 资源

JRException Resource not found at when there is a subreport in Jasper Report

我使用 JasperSoft Studio 制作了一个包含子报表的 jasper 报表。当我在工作室预览它时,它工作正常。但是当我将它添加到我的 java fx 项目中时,我必须将子报表作为参数发送到主报表吗?

当我这样做时,我不断收到一条错误消息,提示未找到子报表资源。 我认为没有任何拼写错误,因为我对几乎所有包含子报告的报告都进行了尝试。.仍然继续出现该错误..

这些是我尝试过的 1.使用jaspersoft studio再次编译报告并将它们再次包含在项目中.. 2. 将 jrxml 文件包含到项目中并使用编译管理器对其进行编译。 3.我读了一些教程和问题,说要将表达式放在主报表中的子报表元素中,如

$P{SUBREPORT_DIR} + "subreport_2.jasper"

但我仍然不断遇到同样的错误,我不知道自己做错了什么 下面是我的代码

用于查看报告的应用程序中的方法

@FXML
    public void btnMarkingSchemeClicked(ActionEvent actionEvent) {
        try {

//            JasperReport compiledReport = (JasperReport) JRLoader.loadObject(ViewAssignmentsController.class.getResourceAsStream("/edu/ijse/gdse41/ams/reports/MarkingScheme.jasper"));
            JasperReport compiledReport = JasperCompileManager.compileReport(ViewAssignmentsController.class.getResourceAsStream("/edu/ijse/gdse41/ams/reports/MarkingScheme.jrxml"));
            HashMap<String, Object> parameters = new HashMap<>();
            parameters.put("assignID", aid);
            parameters.put("course", cmbCourse.getSelectionModel().getSelectedItem().toString());
            parameters.put("subject", cmbSub.getSelectionModel().getSelectedItem().toString());
            parameters.put("semester", cmbSem.getSelectionModel().getSelectedItem().toString());
            parameters.put("assign", ctrlAssign.search(aid).getAssignName());
//            JasperReport subReport = (JasperReport) JRLoader.loadObject(ViewAssignmentsController.class.getResourceAsStream("/edu/ijse/gdse41/ams/reports/Answers.jasper"));
            JasperReport subReport = JasperCompileManager.compileReport(ViewAssignmentsController.class.getResourceAsStream("/edu/ijse/gdse41/ams/reports/Answers.jrxml"));
            parameters.put("subReport", subReport);
            JasperPrint fillReport = JasperFillManager.fillReport(compiledReport, parameters, ConnectionFactory.getInstance().getConnection());
            JasperViewer.viewReport(fillReport, false);

        } catch (JRException | SQLException | ClassNotFoundException ex) {
            Logger.getLogger(ViewAssignmentsController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

我的主要报告来源 -- MarkingScheme.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-06-02T12:52:20 -->
<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="MarkingScheme" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c71b946b-2ba2-4208-97f8-f32e8bcf3fb4">
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="amsAdapter"/>
    <parameter name="course" class="java.lang.String"/>
    <parameter name="subject" class="java.lang.String"/>
    <parameter name="semester" class="java.lang.String"/>
    <parameter name="assign" class="java.lang.String"/>
    <parameter name="assignID" class="java.lang.String">
        <defaultValueExpression><![CDATA[$P{assignID}]]></defaultValueExpression>
    </parameter>
    <parameter name="subReport" class="net.sf.jasperreports.engine.JasperReport">
        <parameterDescription><![CDATA[]]></parameterDescription>
        <defaultValueExpression><![CDATA[$P{subReport}]]></defaultValueExpression>
    </parameter>
    <queryString language="SQL">
        <![CDATA[SELECT ams.question.qid,
    ams.question.question,
    ams.assign_ques.aid
FROM ams.assign_ques
    INNER JOIN ams.question ON 
     ams.assign_ques.qid = ams.question.qid and ams.assign_ques.aid=$P{assignID}]]>
    </queryString>
    <field name="qid" class="java.lang.String"/>
    <field name="question" class="java.lang.String"/>
    <field name="aid" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement mode="Opaque" x="0" y="0" width="555" height="79" forecolor="#FFFFFF" backcolor="#113891" uuid="52ed193a-ed11-46ba-a09f-1864b843df96"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font size="29" isBold="true"/>
                </textElement>
                <text><![CDATA[   Marking Scheme]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="80" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="80" height="40" uuid="55b35bb0-5ea8-4417-88be-0083cfee5037"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font size="14"/>
                </textElement>
                <text><![CDATA[Course]]></text>
            </staticText>
            <staticText>
                <reportElement x="0" y="40" width="80" height="40" uuid="1dc04c2d-9cde-4905-9650-03cc96d406e3"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font size="14"/>
                </textElement>
                <text><![CDATA[Semester]]></text>
            </staticText>
            <staticText>
                <reportElement x="260" y="0" width="100" height="40" uuid="4de2dbe2-bb88-4dbf-88d7-16b36bba8fb4"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font size="14"/>
                </textElement>
                <text><![CDATA[Subject]]></text>
            </staticText>
            <staticText>
                <reportElement x="260" y="40" width="100" height="40" uuid="bb91c5d3-4cde-45eb-8083-012fe6183396"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font size="14"/>
                </textElement>
                <text><![CDATA[Assignment]]></text>
            </staticText>
            <textField>
                <reportElement x="90" y="0" width="160" height="40" forecolor="#123278" uuid="8016c655-1bea-480a-9179-3da4e1e930ad"/>
                <textElement verticalAlignment="Middle">
                    <font size="13"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{course}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="370" y="0" width="170" height="40" forecolor="#123278" uuid="23e58b4b-0bf1-41ae-89f0-48808a0abdd9"/>
                <textElement verticalAlignment="Middle">
                    <font size="13"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{subject}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="90" y="40" width="160" height="40" forecolor="#123278" uuid="650c412a-0c42-4dea-a96f-35d03c11c926"/>
                <textElement verticalAlignment="Middle">
                    <font size="13"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{semester}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="370" y="40" width="170" height="40" forecolor="#123278" uuid="c8cbea5b-afb3-4c85-a96a-156d1c82fb50"/>
                <textElement verticalAlignment="Middle">
                    <font size="13"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{assign}]]></textFieldExpression>
            </textField>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="30" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="30" uuid="f7b84a13-8e80-4c9c-8888-3b081f9d0f55"/>
                <textElement>
                    <font size="16" isBold="true"/>
                </textElement>
                <text><![CDATA[Questions]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="41" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="80" height="20" uuid="9f05a7d2-ac5f-4412-b26b-ab37033cc0fe"/>
                <textElement verticalAlignment="Middle">
                    <font size="12"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{qid}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="0" width="440" height="20" uuid="a299bfe0-a0d1-4aae-8f75-5b58d24b1760"/>
                <textElement verticalAlignment="Middle">
                    <font size="12"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{question}]]></textFieldExpression>
            </textField>
            <subreport>
                <reportElement x="100" y="20" width="440" height="20" uuid="72f8c7c2-82ca-48d3-bb89-d5bb4846e1bc"/>
                <subreportParameter name="qid">
                    <subreportParameterExpression><![CDATA[$F{qid}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["Answers.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

我的子报告来源 -- Answers.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-06-02T12:53:52 -->
<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="Answers" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="142adbc1-da50-458b-b076-ba8aa716124f">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="amsAdapter"/>
    <property name="com.jaspersoft.studio.data.sql.tables" value="YW1zLmFuc3dlciAsMTUsMTUsOGE1MjQ1NzQtOGY1ZS00ZWY4LWFiMDAtOWVjYmVmNmU0MTFjOw=="/>
    <parameter name="qid" class="java.lang.String">
        <defaultValueExpression><![CDATA[$P{qid}]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT ams.answer.answer_id,
    ams.answer.answer,
    ams.answer.correctness,
    ams.answer.qid
FROM ams.answer where ams.answer.qid=$P{qid} and ams.answer.correctness=1]]>
    </queryString>
    <field name="answer_id" class="java.lang.String"/>
    <field name="answer" class="java.lang.String"/>
    <field name="correctness" class="java.lang.Boolean"/>
    <field name="qid" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="80" height="20" uuid="517ba9e8-6637-4808-8517-1485d4125971"/>
                <textElement>
                    <font size="12"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{answer_id}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="90" y="0" width="460" height="20" uuid="4384510c-5dea-47a0-b24b-d4b3bd4088fc"/>
                <textElement>
                    <font size="12"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{answer}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

我得到的错误

net.sf.jasperreports.engine.JRException: Resource not found at: Answers.jasper. at net.sf.jasperreports.repo.RepositoryUtil.getResourceFromLocation(RepositoryUtil.java:153) at net.sf.jasperreports.repo.RepositoryUtil.getReport(RepositoryUtil.java:112) at net.sf.jasperreports.engine.fill.JRFillSubreport.loadReport(JRFillSubreport.java:398) at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateReport(JRFillSubreport.java:365) at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:427) at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:341) at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:381) at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:500) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2022) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:748) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:255) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:115) at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:582) at net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:396) at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:90) at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:456) at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:863) at edu.ijse.gdse41.ams.view.controller.ViewAssignmentsController.btnMarkingSchemeClicked(ViewAssignmentsController.java:174) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769) at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) at javafx.event.Event.fireEvent(Event.java:198) at javafx.scene.Node.fireEvent(Node.java:8413) at javafx.scene.control.Button.fire(Button.java:185) at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:96) at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:89) at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) at javafx.event.Event.fireEvent(Event.java:198) at javafx.scene.Scene$MouseHandler.process(Scene.java:3757) at javafx.scene.Scene$MouseHandler.access00(Scene.java:3485) at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent4(GlassViewEventHandler.java:417) at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416) at com.sun.glass.ui.View.handleMouseEvent(View.java:555) at com.sun.glass.ui.View.notifyMouse(View.java:937) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191) at java.lang.Thread.run(Thread.java:745)

这是因为即使您创建了一个名为 "subReport" 的参数,但它还没有在任何地方使用。

           <subreport>
            <reportElement x="100" y="20" width="440" height="20" uuid="72f8c7c2-82ca-48d3-bb89-d5bb4846e1bc"/>
            <subreportParameter name="qid">
                <subreportParameterExpression><![CDATA[$F{qid}]]></subreportParameterExpression>
            </subreportParameter>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            <subreportExpression><![CDATA["Answers.jasper"]]></subreportExpression>
        </subreport>

看到了吗?它在主报告中仍然是“Answers.jasper”。这就是它在 Jasper Studio 中运行良好的原因。让我们把它改成,

           <subreport>
            <reportElement x="100" y="20" width="440" height="20" uuid="72f8c7c2-82ca-48d3-bb89-d5bb4846e1bc"/>
            <subreportParameter name="qid">
                <subreportParameterExpression><![CDATA[$F{qid}]]></subreportParameterExpression>
            </subreportParameter>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            <subreportExpression><![CDATA[$P{subReport}]]></subreportExpression>
        </subreport>

我很确定我不是教你使用 Jasper 的人:) 希望它能奏效

在 MarkingScheme.jrxml 中更改为

<subreportExpression><![CDATA["Answers.jasper"]]></subreportExpression>

<subreportExpression><![CDATA[$P{subReport}]]></subreportExpression>

更改 class 添加 Answers.jasper 包括其路径。

parameters.put("subReport", "/edu/ijse/gdse41/ams/reports/Answers.jasper");

路径必须从根上下文 (/) 开始完成。

您的 subreportExpression 标签应该是 $P{SUBREPORT_DIR} + "Answer.jasper",如下所示:

<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "Answer.jasper"]]></subreportExpression>

并且您需要根据您的上下文为 $P{SUBREPORT_DIR}

动态提供路径

我在 Grails 上使用 Groovy!基本上,报告遗漏了 jrxml 中 SUBREPORT_DIR 的参数值。 我的 报告和子报告 web-app/reports 文件夹中。因此,在使用 subReport 生成报告时,我添加了如下路径值:

params.put('SUBREPORT_DIR', getReportDirectory() + File.separator)

其中 getReportDirectory() 函数如下所示:

def getReportDirectory() {
        String REPORT_DIRECTORY = null;
        if (!REPORT_DIRECTORY) {
            File reportFolder = ApplicationHolder.application.parentContext.getResource('/reports').file;
            REPORT_DIRECTORY = reportFolder.absolutePath;
        }

       return REPORT_DIRECTORY;
}