当前位置: 首页 > 知识库问答 >
问题:

为什么JRBeanCollectionDataSource使用jr:List组件传递null字段?

柳昊焱
2023-03-14
public class Medicine {

    private String medicineName;
    private String schedule;
    private String date;
    private String hour;
    private String dosage;
    private String taken;

    public String getMedicineName() {
        return medicineName;
    }
    public void setMedicineName(String medicineName) {
        this.medicineName = medicineName;
    }
    public String getSchedule() {
        return schedule;
    }
    public void setSchedule(String schedule) {
        this.schedule = schedule;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getHour() {
        return hour;
    }
    public void setHour(String hour) {
        this.hour = hour;
    }
    public String getDosage() {
        return dosage;
    }
    public void setDosage(String dosage) {
        this.dosage = dosage;
    }
    public String getTaken() {
        return taken;
    }
    public void setTaken(String taken) {
        this.taken = taken;
    }

}

这就是我如何发送数据报告。

try {
        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource("reports/medipost.jrxml").getFile());
           JasperReport jasperReport = JasperCompileManager
                   .compileReport(file.getAbsolutePath());
           MediPostDAO mediPostDAO = new MediPostDAO();
           Map<String, Object> parameters = mediPostDAO.getMedipostData(id);
           List<Medicine> medicineList = new MedicineDAO().getMedicineList(id);
           JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(medicineList);
           JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
                   parameters, ds);

           try {
            JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
            response.setContentType("application/pdf");

        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (JRException e) {
        e.printStackTrace();
    }

这是一个jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.1.final using     JasperReports Library version 6.2.1  -->
<!-- 2016-04-20T18:08:25 -->
<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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2e9583be-f081-46a8-ab4f-06825850ee37">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
        <topPen lineWidth="0.5" lineColor="#000000"/>
        <leftPen lineWidth="0.5" lineColor="#000000"/>
        <bottomPen lineWidth="0.5" lineColor="#000000"/>
        <rightPen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
        <topPen lineWidth="0.5" lineColor="#000000"/>
        <leftPen lineWidth="0.5" lineColor="#000000"/>
        <bottomPen lineWidth="0.5" lineColor="#000000"/>
        <rightPen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
        <topPen lineWidth="0.5" lineColor="#000000"/>
        <leftPen lineWidth="0.5" lineColor="#000000"/>
        <bottomPen lineWidth="0.5" lineColor="#000000"/>
        <rightPen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="Table 1_TH" mode="Opaque" backcolor="#F0F8FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
        <topPen lineWidth="0.5" lineColor="#000000"/>
        <leftPen lineWidth="0.5" lineColor="#000000"/>
        <bottomPen lineWidth="0.5" lineColor="#000000"/>
        <rightPen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="Table 1_CH" mode="Opaque" backcolor="#BFE1FF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
        <topPen lineWidth="0.5" lineColor="#000000"/>
        <leftPen lineWidth="0.5" lineColor="#000000"/>
        <bottomPen lineWidth="0.5" lineColor="#000000"/>
        <rightPen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<style name="Table 1_TD" mode="Opaque" backcolor="#FFFFFF">
    <box>
        <pen lineWidth="0.5" lineColor="#000000"/>
        <topPen lineWidth="0.5" lineColor="#000000"/>
        <leftPen lineWidth="0.5" lineColor="#000000"/>
        <bottomPen lineWidth="0.5" lineColor="#000000"/>
        <rightPen lineWidth="0.5" lineColor="#000000"/>
    </box>
</style>
<subDataset name="Dataset1" uuid="de015a45-b31b-4e76-b602-c48d2099b2f0">
    <parameter name="medicineName" class="java.lang.String"/>
    <parameter name="schedule" class="java.lang.String"/>
    <parameter name="date" class="java.lang.String"/>
    <parameter name="hour" class="java.lang.String"/>
    <parameter name="dosage" class="java.lang.String"/>
    <parameter name="Taken" class="java.lang.String"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="date" class="java.lang.String"/>
    <field name="medicineName" class="java.lang.String"/>
    <field name="dosage" class="java.lang.String"/>
    <field name="hour" class="java.lang.String"/>
    <field name="taken" class="java.lang.String"/>
    <field name="schedule" class="java.lang.String"/>
</subDataset>
<parameter name="disease" class="java.lang.String"/>
<parameter name="doctor" class="java.lang.String"/>
<parameter name="pharmacy" class="java.lang.String"/>
<parameter name="symptom" class="java.lang.String"/>
<parameter name="profileName" class="java.lang.String">
    <parameterDescription><![CDATA[]]></parameterDescription>
</parameter>
<parameter name="age" class="java.lang.String"/>
<parameter name="gender" class="java.lang.String"/>
<parameter name="bloodGroup" class="java.lang.String"/>
<queryString>
    <![CDATA[]]>
</queryString>
<background>
    <band splitType="Stretch"/>
</background>
<pageHeader>
    <band height="101">
        <textField>
            <reportElement x="0" y="1" width="180" height="20" uuid="38b835c7-7f37-4208-9e17-f1265afe7abd"/>
            <textElement>
                <font fontName="Times New Roman" size="16" isBold="true"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{profileName}]]></textFieldExpression>
        </textField>
        <staticText>
            <reportElement x="0" y="35" width="30" height="20" uuid="9c9ddeee-18b9-4d0c-aad9-32e8e00d375a"/>
            <textElement>
                <font fontName="Times New Roman" size="14" isBold="true"/>
            </textElement>
            <text><![CDATA[Age]]></text>
        </staticText>
        <textField>
            <reportElement x="30" y="36" width="17" height="19" uuid="35a16a58-bef5-4753-a3a6-5478eeda8aac"/>
            <textElement>
                <font fontName="Times New Roman" size="14" isBold="true"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{age}]]></textFieldExpression>
        </textField>
        <line>
            <reportElement x="51" y="35" width="1" height="20" uuid="7962e3e8-0f3d-4096-8145-569d5044f401">
                <property name="com.jaspersoft.studio.unit.x" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
            </reportElement>
        </line>
        <staticText>
            <reportElement x="55" y="35" width="50" height="20" uuid="2d568652-4551-40b8-9a0f-b504cced79d5"/>
            <textElement>
                <font fontName="Times New Roman" size="14" isBold="true"/>
            </textElement>
            <text><![CDATA[Gender]]></text>
        </staticText>
        <textField>
            <reportElement x="102" y="35" width="46" height="20" uuid="1755ba6d-a486-4adf-83da-1a06af7c1369"/>
            <textElement verticalAlignment="Middle">
                <font fontName="Times New Roman" size="14" isBold="true"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{gender}]]></textFieldExpression>
        </textField>
        <line>
            <reportElement x="150" y="35" width="1" height="20" uuid="6371ec0f-d26f-4e82-88ec-26a724c40d54">
                <property name="com.jaspersoft.studio.unit.x" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
            </reportElement>
        </line>
        <staticText>
            <reportElement x="150" y="36" width="80" height="20" uuid="5634ce23-b218-4ffe-8237-cc49ceeea862"/>
            <textElement>
                <font fontName="Times New Roman" size="14" isBold="true"/>
            </textElement>
            <text><![CDATA[Blood Group]]></text>
        </staticText>
        <textField>
            <reportElement x="230" y="36" width="51" height="21" uuid="45df73f2-4a33-47e9-8d0b-804c99946bcc"/>
            <textElement>
                <font fontName="Times New Roman" size="14" isBold="true"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{bloodGroup}]]></textFieldExpression>
        </textField>
        <staticText>
            <reportElement x="-5" y="57" width="63" height="20" uuid="ea748f7d-f4d0-4e9e-8379-a52e74b0cbf2"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
                <font fontName="Times New Roman" size="12" isBold="true"/>
            </textElement>
            <text><![CDATA[Condition]]></text>
        </staticText>
        <textField>
            <reportElement x="52" y="57" width="90" height="20" forecolor="#F51511" uuid="ae53e0e0-d755-4cfd-9e7a-b3bbde881f9d"/>
            <box>
                <topPen lineColor="#F22824"/>
                <leftPen lineColor="#F22824"/>
                <bottomPen lineColor="#F22824"/>
                <rightPen lineColor="#F22824"/>
            </box>
            <textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
                <font size="12" isBold="true"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{disease}]]></textFieldExpression>
        </textField>
        <line>
            <reportElement x="140" y="55" width="1" height="20" uuid="1f89c71f-d8a3-4dee-9943-417470344334">
                <property name="com.jaspersoft.studio.unit.x" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
            </reportElement>
        </line>
        <staticText>
            <reportElement x="142" y="57" width="63" height="20" uuid="e887596a-a3b6-4285-afca-4d96b9c27f64"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
                <font fontName="Times New Roman" size="12" isBold="true"/>
            </textElement>
            <text><![CDATA[Symptoms]]></text>
        </staticText>
        <textField>
            <reportElement x="200" y="57" width="100" height="20" forecolor="#F21511" uuid="3ba4e883-6b4e-44a8-8360-b1acff38e3d9"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
                <font fontName="Times New Roman" size="12" isBold="true"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{symptom}]]></textFieldExpression>
        </textField>
        <line>
            <reportElement x="300" y="57" width="1" height="20" uuid="3a76da0b-a958-4dee-bb9a-560098b496d6">
                <property name="com.jaspersoft.studio.unit.x" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
            </reportElement>
        </line>
        <staticText>
            <reportElement x="301" y="57" width="63" height="20" uuid="5fcd5ba3-a11a-45e3-9e22-56b272211390"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
                <font fontName="Times New Roman" size="12" isBold="true"/>
            </textElement>
            <text><![CDATA[Doctors]]></text>
        </staticText>
        <textField>
            <reportElement x="360" y="57" width="100" height="21" forecolor="#FA1D19" uuid="a9f3ff9e-74b8-4112-b1c1-cdca8bd918b3"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
                <font fontName="Times New Roman" size="12" isBold="true"/>
            </textElement>
            <textFieldExpression><![CDATA[$P{doctor}]]></textFieldExpression>
        </textField>
        <line>
            <reportElement x="-3" y="85" width="560" height="1" uuid="489b1135-0cd0-4d47-8ed4-6a689582c264">
                <property name="com.jaspersoft.studio.unit.x" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            </reportElement>
        </line>
        <line>
            <reportElement x="0" y="90" width="281" height="1" uuid="f16072f4-ed14-410c-a6fd-2851b2f88f04">
                <property name="com.jaspersoft.studio.unit.x" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            </reportElement>
        </line>
        <line>
            <reportElement x="0" y="31" width="560" height="1" uuid="d515cf7a-05b5-4a5e-b30c-95b8e79f2381">
                <property name="com.jaspersoft.studio.unit.x" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            </reportElement>
        </line>
    </band>
</pageHeader>
<detail>
    <band height="50" splitType="Stretch">
        <componentElement>
            <reportElement x="0" y="0" width="100" height="30" uuid="33832d43-6539-48db-ae31-fc5836c7f038">
                <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.HorizontalRowLayout"/>
            </reportElement>
            <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                <datasetRun subDataset="Dataset1" uuid="b8e46627-a532-4836-bcb1-56f47d997ef9">
                    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource() ]]></dataSourceExpression>
                </datasetRun>
                <jr:listContents height="30" width="100">
                    <textField>
                        <reportElement x="0" y="0" width="20" height="30" uuid="7985e712-7deb-4f22-869f-2f2ebcd77f59"/>
                        <textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
                    </textField>
                    <textField>
                        <reportElement x="20" y="0" width="16" height="30" uuid="0b0ede12-bd96-46ba-afd8-41581438b6a4"/>
                        <textFieldExpression><![CDATA[$F{medicineName}]]></textFieldExpression>
                    </textField>
                    <textField>
                        <reportElement x="36" y="0" width="16" height="30" uuid="5c597083-edf0-4159-b455-018f9b83f8ee"/>
                        <textFieldExpression><![CDATA[$F{dosage}]]></textFieldExpression>
                    </textField>
                    <textField>
                        <reportElement x="52" y="0" width="16" height="30" uuid="483a1dc0-d6a5-40e6-8e51-ca03cde8a7b1"/>
                        <textFieldExpression><![CDATA[$F{hour}]]></textFieldExpression>
                    </textField>
                    <textField>
                        <reportElement x="68" y="0" width="16" height="30" uuid="2bdf851b-2981-40a2-b3fd-42bd8df3b990"/>
                        <textFieldExpression><![CDATA[$F{taken}]]></textFieldExpression>
                    </textField>
                    <textField>
                        <reportElement x="84" y="0" width="16" height="30" uuid="cf0cf0e7-ec90-42d4-ba3f-fb27563890fe"/>
                        <textFieldExpression><![CDATA[$F{schedule}]]></textFieldExpression>
                    </textField>
                </jr:listContents>
            </jr:list>
        </componentElement>
    </band>
</detail>
</jasperReport>

共有1个答案

丌官丰羽
2023-03-14

这是因为您在将JRBeanCollectionDataSource传递给主报表时,正在为列表组件使用子数据集。因此,主报表具有JRBeanCollectionDataSource,但列表组件具有JREMPTYDataSource

解决办法

>

  • 将列表作为参数传递

    parameter.put("medicineList",new MedicineDAO().getMedicineList(id));
    
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
               parameters, new JREmptyDataSource(1))
    
    <parameter name="medicineList" class="java.util.List" isForPrompting="false"/>
    ....
    <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
      <datasetRun subDataset="Dataset1" uuid="b8e46627-a532-4836-bcb1-56f47d997ef9">
          <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{medicineList}) ]]></dataSourceExpression>
      </datasetRun>
    ....
    

  •  类似资料:
    • 问题内容: 为什么经常被称为 代替 ? W3,MDN和MSDN都声明它是可选的。此外,ActiveX控件似乎不需要参数: 这种做法至少可以追溯到2005年的Google Maps中 ,但被缩小了,没有任何解释: 问题答案: 如果您看一下XMLHttpRequest的旧规范,似乎W3C似乎并不需要在某一点上将该参数设为可选,这可能导致人们提供了一个明确的null值,以防万一。 (搜索“应支持发送”)

    • 假设我们有一个名为的react组件,它非常聪明地在一行中显示了三个Arbityle react组件(甚至只是任意定义的dom元素)。 我们可以通过将react元素设置为状态元素并将它们作为属性传入来实现这一点,如下面的答案所建议的: 这恰恰适用于我想做的事情--我想知道的是--这是不是做事情的错误方式?有没有理由(如表现)不这样做事?

    • 问题内容: 在我的Web应用程序中,我在Apache Tomcat(TomEE)/7.0.37服务器上使用OpenJPA。我使用Netbeans自动生成类(“来自数据库的实体类…”和“来自实体类的会话Bean …”)。在SessionBean(例如UserFacade)上,我想获取EntityManager: 但是当我通过上述方式得到它时,我得到的是空值。当我通过: ecm不为空,还可以 我的pe

    • 本文向大家介绍null == undefined为什么?相关面试题,主要包含被问及null == undefined为什么?时的应答技巧和注意事项,需要的朋友参考一下 参考回答: 要比较相等性之前,不能将null 和 undefined 转换成其他任何值,但 null == undefined 会返回 true 。ECMAScript规范中是这样定义的。

    • 问题内容: 我正在尝试将需要处理的文件数组传递给gulp.src。这就是它所代表的数组。 我发现虽然gulp.src似乎并不喜欢,并且第三个元素没有使其进入最终目标。 我发现当我引入一些通配符时,一切工作正常: 但为什么?与通配符的工作方式有关吗?我已经用谷歌搜索,但找不到。 也许这不是实现globbing的预期目的,但对我而言,以这种方式工作并不有意义。谁能给我一些启示? 问题答案: 当您传递完

    • 我想使用 JRBeanCollectionDataSource 传递列表并显示在数据集 1 中。在通过list1报告之前工作正常。但是当我将list1传递给报告页面时,页面变得空了。我不知道是不是数据源的问题.... .jrxml 文件: