在Jasper Report

时间:2017-06-02 07:27:38

标签: java jasper-reports subreport

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

当我这样做时,我继续收到错误消息,指出未找到子报告资源。 我不认为有任何拼写错误,因为我几乎所有包含子报告的报告都试过这个...仍然继续得到错误..

这些是我尝试的东西 1.使用jaspersoft studio再次编译报告,并将它们再次包含在项目中。 2.将jrxml文件包含到项目中并使用Compile Manager编译它。 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:未找到资源:   Answers.jasper。在   net.sf.jasperreports.repo.RepositoryUtil.getResourceFromLocation(RepositoryUtil.java:153)     在   net.sf.jasperreports.repo.RepositoryUtil.getReport(RepositoryUtil.java:112)     在   net.sf.jasperreports.engine.fill.JRFillSubreport.loadReport(JRFillSubreport.java:398)     在   net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateReport(JRFillSubreport.java:365)     在   net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:427)     在   net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:341)     在   net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:381)     在   net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:500)     在   net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2022)     在   net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:748)     在   net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:255)     在   net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:115)     在   net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:582)     在   net.sf.jasperreports.engine.fill.BaseReportFiller.fill(BaseReportFiller.java:396)     at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:90)     在   net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:456)     在   net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:863)     在   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)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)at   sun.reflect.GeneratedMethodAccessor1.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在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 at   javafx.fxml.FXMLLoader $ ControllerMethodEventHandler.handle(FXMLLoader.java:1657)     在   com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)     在   com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)     在   com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)     在   com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)     在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)     在   com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:96)     在   com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:89)     在   com.sun.javafx.event.CompositeEventHandler $ NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)     在   com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)     在   com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)     在   com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)     在   com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在   com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在   com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)     在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 at   javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)at   javafx.scene.Scene $ ScenePeerListener.mouseEvent(Scene.java:2494)at at   com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:381)     在   com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:295)     在java.security.AccessController.doPrivileged(Native Method)at   com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda $ handleMouseEvent $ 354(GlassViewEventHandler.java:417)     在   com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)     在   com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)     在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)     在java.lang.Thread.run(Thread.java:745)

3 个答案:

答案 0 :(得分:2)

您的 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;
}

答案 1 :(得分:1)

这是因为即使您创建了一个名为&#34; subReport&#34;的参数。它没有在任何地方使用过。

           <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>

请参阅?它仍然是&#34; Answers.jasper &#34;在主要报告中。这就是为什么它在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的人:) 让我们希望它会起作用

答案 2 :(得分:1)

在MarkingScheme.jrxml中更改

session_id: aea6434a454efb40851d035612307b25
array(0) { } uid:
session.cookie_domain: 
session file: /var/customers/tmp/censored//sess_aea6434a454efb40851d035612307b25 
size: 0 

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

更改添加Answers.jasper的类,包括其路径。

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

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