Java 报告设计无效。未找到字段 Jasper 报告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30952574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:24:38  来源:igfitidea点击:

Report design not valid. Field not found Jasper Reports

javajasper-reports

提问by Dilantha

Im trying to create a basic jasper report with JRBeanCollectionDataSource. In there im having a list of objects inside the javabean.

我正在尝试使用JRBeanCollectionDataSource. 在那里,我有一个 javabean 中的对象列表。

public class Course {

    private int id;
    private List<Student> students;
}

Student object looks like

学生对象看起来像

public class Student {

    private String name;
    private int id;
}

I want to print student information inside my report. This is how my jrxml looks like

我想在我的报告中打印学生信息。这就是我的 jrxml 的样子

 <subDataset name="dataset1" uuid="09015d96-ad5a-4fed-aa9e-19d25e02e205">
 <field name="students" class="java.util.List">
  <fieldDescription><![CDATA[students]]></fieldDescription>
 </field>
</subDataset>

<field name="id" class="java.lang.Integer"/>
<field name="students" class="java.util.List"/>
<field name="name" class="java.lang.String"/>

<componentElement>
                <reportElement x="200" y="0" width="400" height="20"/>
                <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" printOrder="Vertical">
                    <datasetRun subDataset="dataset1">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{students})]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="20" width="400">
                        <textField>
                            <reportElement x="0" y="0" width="100" height="20"/>
                            <box leftPadding="10">
                                <topPen lineWidth="1.0"/>
                                <leftPen lineWidth="1.0"/>
                                <bottomPen lineWidth="1.0"/>
                                <rightPen lineWidth="1.0"/>
                            </box>
                            <textElement/>
                            <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                        </textField>

                    </jr:listContents>
                </jr:list>
            </componentElement>

But when i run this im getting

但是当我运行这个时,我得到了

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

Im a beginner to jasper reports can anyone please tell me what am i doing wrong here. Thanks

我是 jasper 报告的初学者,任何人都可以告诉我我在这里做错了什么。谢谢

采纳答案by Dilantha

Fount out the issue. name attribute should defined inside the subdataset. Otherwise it wont work

找出问题。name 属性应该在subdataset. 否则它不会工作

<subDataset name="dataset1" uuid="09015d96-ad5a-4fed-aa9e-19d25e02e205">
     <field name="name" class="java.lang.String"/>
</subDataset>

回答by Turbero

Try with a subreport. I launched your example in my local database without any problem.

尝试使用子报表。我在本地数据库中启动了您的示例,没有任何问题。

This is my list of student objects:

这是我的学生对象列表:

private List<Student> getList(){
    List<Student> students = new ArrayList<Student>();
    Connection con = ....; //Retrieve your connection the way you want
    ResultSet rs = con.createStatement().executeQuery("select name, id from students order by id");
    while (rs.next()){
        students.add(new Student(rs.getString(1), rs.getInt(2)));
    }
    con.close();
    return students;
}

This is how I passed my JRBeanCollectionDataSourceobject from my Java program:

这是我JRBeanCollectionDataSource从 Java 程序传递对象的方式:

Map<String, Object> params = new HashMap<String, Object>();
params.put("SUBREPORT_DATASOURCE", new JRBeanCollectionDataSource(getList()));

String jasperPath = "....."; //where you have your compiled jasper report file
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath, params, new JREmptyDataSource(1));

This is the xlm main report "reportStudent.jrxml":

这是 xlm 主报告“reportStudent.jrxml”:

    <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="reportStudent" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="SUBREPORT_DATASOURCE" class="net.sf.jasperreports.engine.JRDataSource" isForPrompting="false">
        <defaultValueExpression><![CDATA[]]></defaultValueExpression>
    </parameter>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="125" splitType="Stretch">
            <subreport>
                <reportElement x="0" y="0" width="555" height="125"/>
                <dataSourceExpression><![CDATA[$P{SUBREPORT_DATASOURCE}]]></dataSourceExpression>
                <subreportExpression><![CDATA["./reportStudent_subreport1.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

And this is the one for the subreport "reportStudent_subreport1.jrxml":

这是子报表“reportStudent_subreport1.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="reportStudent_subreport1" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <property name="ireport.zoom" value="1.771561000000001"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <field name="id" class="java.lang.String"/>
    <field name="name" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement x="66" y="0" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[id]]></text>
            </staticText>
            <staticText>
                <reportElement x="212" y="0" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[name]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="66" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="212" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

This is for just one list of students. You can iterate inside the main report and print your course id and students subreport in the detail band with just a little changes. Hope it helps to start with.

这仅适用于一份学生名单。您可以在主报告中迭代并在细节带中打印您的课程 ID 和学生子报告,只需稍作更改。希望它有助于开始。

回答by MKB

You have to define the fields before using it.

您必须在使用之前定义字段。

In your jrxml, you have three field defined studentsin the subDataSet, idand students. But you haven't defined nameand using it in your jrxmland that's why you are getting this exception.

在您的 中jrxml,您students在 subDataSet 中定义了三个字段,id并且students. 但是您还没有定义name和使用它jrxml,这就是您收到此异常的原因。

Try defining name, like

尝试定义name,比如

<field name="name" class="java.lang.String"/>