gpt4 book ai didi

java - .jrxml 到 .jasper 编译错误字段未找到异常

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

嗨,我是 jasper 报告的新手。我正在使用 ireport 来设计报告。我创建了一个简单的报告设计,其中有 2 个字段名称、地址。

public class R {
private String name;
private String addr;

public R(String name, String addr) {
super();
this.name = name;
this.addr = addr;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}

.jrxml 文件

<?xml version="1.0" encoding="UTF-8"?>
<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="R" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4fd2f78e-344d-4371-ba1e-f86c2a81ef75">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch">
<staticText>
<reportElement x="296" y="41" width="228" height="20" uuid="19aa39a0-45cf-4b36-b327-25b640f81d8a"/>
<textElement textAlignment="Center"/>
<text><![CDATA[Address]]></text>
</staticText>
<staticText>
<reportElement x="36" y="41" width="215" height="20" uuid="baba8372-2215-439d-8b20-fd4ab054bbe5"/>
<textElement textAlignment="Center"/>
<text><![CDATA[Name]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="296" y="0" width="228" height="20" uuid="c4de17e6-e5bc-4454-b38e-cc50acf18f0b"/>
<textFieldExpression><![CDATA[$F{addr}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="36" y="0" width="215" height="20" uuid="8283d8b6-a288-48ec-bd4a-36c2875f5cff"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>

这里我使用数组列表作为数据源。当我单击报告上的编译按钮时,它显示错误

net.sf.jasperreports.engine.design.JRValidationException: Report design not valid :       1. Field not found : addr      2. Field not found : name

java代码

public void runReport(String fileName, String outFileName) throws JRException 
{

List<R> list = new ArrayList<R>();

Map parameters = new HashMap();
list.add(new R("a1" ,"a2"));
list.add(new R("b1" ,"b2"));
list.add(new R("c1" ,"c2"));

JasperPrint print = JasperFillManager.fillReport( fileName, parameters, new JRBeanCollectionDataSource(list));
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(
JRExporterParameter.OUTPUT_FILE_NAME,outFileName);
exporter.setParameter(
JRExporterParameter.JASPER_PRINT, print);
JasperExportManager.exportReportToPdfFile(print, outFileName);
print = null;
exporter = null;

}

最佳答案

您应该添加到 jrxml 字段:

<field name="addr" class="java.lang.String">
<fieldDescription><![CDATA[addr]]></fieldDescription></field>

您可以通过单击“字段”并选择“创建字段”来完成此操作。

关于java - .jrxml 到 .jasper 编译错误字段未找到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26709636/

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