Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在JasperReports中获取列表的第一个元素?_Java_Jasper Reports - Fatal编程技术网

Java 如何在JasperReports中获取列表的第一个元素?

Java 如何在JasperReports中获取列表的第一个元素?,java,jasper-reports,Java,Jasper Reports,我有一个包含嵌套对象列表的对象。我只想得到列表的第一个元素,并显示它的一个属性 例如: public class Person { List<Phone> phones; } public class Phone { String type; String number; } 我还尝试了以下方法: <field name="phones" class="java.util.List" /> <field name="phoneFirst"

我有一个包含嵌套对象列表的对象。我只想得到列表的第一个元素,并显示它的一个属性

例如:

public class Person {
    List<Phone> phones;
}

public class Phone {
    String type;
    String number;
}
我还尝试了以下方法:

<field name="phones" class="java.util.List" />
<field name="phoneFirst" class="my.package.Phone">
    <fieldDescription><![CDATA[$F{phones}.get(0)]]></fieldDescription>
</field>
<field name="phoneFirstNumber" class="java.lang.String">
    <fieldDescription><![CDATA[$F{phoneFirst}.getNumber()]]></fieldDescription>
</field>

结果:
net.sf.jasperreports.engine.JRException:从bean检索字段值时出错:$F{phones}.get(0)


那么,我怎样才能告诉JasperReports只获取集合的第一个元素呢?

我会用Java做这件事:

public class Person {
    List<Phone> phones;
    ...
    public Phone getFirstPhone() { 
        if(this.phones != null) {
            return this.phones.get(0);
        }
        return null;
    }
    ...
}
公共类人物{
列出电话;
...
公用电话getFirstPhone(){
if(this.phones!=null){
返回此。电话。获取(0);
}
返回null;
}
...
}
然后在JRXML中出现如下内容:

<field name="firstPhoneNumber" class="java.lang.String">
    <fieldDescription><![CDATA[$F{firstPhone}.getNumber()]</fieldDescription>
</field>


我想使用自动生成的复杂xml webservice响应对象,因此无法修改该类。我希望我能在jreports中实现这一点,因为我不必引入jasper transfer对象……我很想知道我的答案有什么不对,以保证3次否决票。原始问题中没有任何内容表明Person类不能修改。
<field name="firstPhoneNumber" class="java.lang.String">
    <fieldDescription><![CDATA[$F{firstPhone}.getNumber()]</fieldDescription>
</field>