java JRException当碧玉报表中存在子报表时,在未找到资源

7lrncoxx  于 2023-05-27  发布在  Java
关注(0)|答案(3)|浏览(106)

我使用JasperSoft Studio制作了一个包含子报表的碧玉报表。当我在工作室预览它时,它工作得很好。但是当我将它添加到我的java fx项目中时,我必须将子报表作为参数发送到主报表,对吗?
当我这样做的时候,我一直得到一个错误,说找不到子报表资源。我不认为有任何拼写错误,因为我已经尝试了几乎所有的报告,包括子报告。仍然继续得到那个错误。
这是我尝试过的东西1。使用jaspersoft studio再次编译了报告,并将其再次包含在项目中。2.将jrxml文件包含到项目中,并使用编译管理器对其进行编译。3.我读过一些教程和问题,上面说要把表达式放到主报表中的subreport元素中作为
$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$1.handle(BehaviorSkinBase.java:96) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.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.access$1500(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$handleMouseEvent$354(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$null$148(WinApplication.java:191) at java.lang.Thread.run(Thread.java:745)

jhdbpxl9

jhdbpxl91#

这是因为即使您创建了一个名为“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.碧玉”。这就是为什么它在碧玉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>

我很确定我不是教你使用碧玉的人:)让我们希望它能起作用

dba5bblo

dba5bblo2#

你的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;
}
5cg8jx4n

5cg8jx4n3#

在MarkingScheme.jrxml中,将

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

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

更改类,添加Answers.碧玉,包括其路径。

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

必须从根上下文(/)开始填写路径。

相关问题