gpt4 book ai didi

java - 为什么 JRBeanCollectionDataSource 使用 jr :list component? 给出传递 List 的空字段

转载 作者:行者123 更新时间:2023-12-01 10:01:54 25 4
gpt4 key购买 nike

我正在创建一个 jasper 报告,其中包含标题和元素列表。我通过 JRBeanCollectionDataSource 将自定义对象的数组列表传递给 jasper 报告我已使用列表组件。但它总是返回空字段,尽管我在列表中有数据。

这是我的 Bean 类:

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>

最佳答案

这是因为您在将 JRBeanCollectionDataSource 传递到主报表时为列表组件使用了子数据集。因此,主报告具有 JRBeanCollectionDataSource,但您的列表组件具有 JREmptyDataSource

解决方案

  • 将列表作为参数传递

    parameter.put("medicineList",new MedicineDAO().getMedicineList(id));
  • 通过 JREmptyDataSource到报告(您使用此数据源,请传递 1 条记录以显示详细信息区域一次)

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
    parameters, new JREmptyDataSource(1))
  • 将新参数添加到 jrxml 并将正确的数据源传递到 jr:list

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

此外,您对参数也有同样的困惑,主报告中使用的参数(不在列表组件内部)需要在主报告级别(不在子数据集内部)定义,组件中使用的参数定义是正确的它们位于子数据集中,但您还需要将它们传递给组件。

关于java - 为什么 JRBeanCollectionDataSource 使用 jr :list component? 给出传递 List 的空字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36744136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com