JRBeanCollectionDataSource:如何显示来自 JavaBean 的 java.util.List 中的数据? [英] JRBeanCollectionDataSource: How to show data from the java.util.List from JavaBean?

查看:13
本文介绍了JRBeanCollectionDataSource:如何显示来自 JavaBean 的 java.util.List 中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 JavaBean 包含 java.util.List.

Userinfo {
    private String username;
    private String password;
    List<Address> listAddress;
}

如何在Detail区域显示这个List的数据?

How to show the data of this List in the Detail band?

推荐答案

这是工作示例.

这个样本的要点:

Here is the working sample.

The key points of this sample:

  • _THIS 表达式的使用;
  • Detail
  • 中使用List (jr:list)组件

生成报告的代码片段:

public static void testBuildPdf() {
    try {
        Map<String, Object> params = new HashMap<String, Object>();
        JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, getDataSource());

        JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static JRDataSource getDataSource() {
    Collection<BeanWithList> coll = new ArrayList<BeanWithList>();
    coll.add(new BeanWithList(Arrays.asList("London", "Paris"), 1));
    coll.add(new BeanWithList(Arrays.asList("London", "Madrid", "Moscow"), 2));
    coll.add(new BeanWithList(Arrays.asList("Rome"), 3));

    return new JRBeanCollectionDataSource(coll);
}

JavaBean 代码:

The JavaBean code:

public class BeanWithList {

    // The member's name can be any. The JR engine is using public getter for extracting field's value
    private List<String> cities;
    private Integer id;

    public BeanWithList(List<String> cities, Integer id) {
        this.cities = cities;
        this.id = id;
    }

    // getter should be public    
    public List<String> getCities() {
        return this.cities;
    }

    public Integer getId() {
        return this.id;
    }
}

jrxml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
    <subDataset name="dataset1">
        <field name="city" class="java.lang.String">
            <fieldDescription><![CDATA[_THIS]]></fieldDescription>
        </field>
    </subDataset>
    <field name="id" class="java.lang.Integer"/>
    <field name="cities" class="java.util.Collection"/>
    <title>
        <band height="103" splitType="Stretch">
            <staticText>
                <reportElement x="138" y="28" width="258" height="20"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Bean with List sample]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="20">
            <staticText>
                <reportElement x="0" y="0" width="100" height="20"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true" isUnderline="false"/>
                </textElement>
                <text><![CDATA[Id]]></text>
            </staticText>
            <staticText>
                <reportElement x="100" y="0" width="100" height="20"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true" isUnderline="false"/>
                </textElement>
                <text><![CDATA[City name]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement stretchType="RelativeToTallestObject" 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{id}]]></textFieldExpression>
            </textField>
            <componentElement>
                <reportElement x="100" 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{cities})]]></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{city}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>
        </band>
    </detail>
</jasperReport>

结果将是:

其他相关问题是 如何在 iReport 中打印包含在另一个列表中的字符串列表? 问题和 传递原始列表类型对象作为子报表的数据源 问题.

Other related questions are the How do I print a list of strings contained within another list in iReport? question and Passing the List of primitive type objects as datasource for subreport question.

这篇关于JRBeanCollectionDataSource:如何显示来自 JavaBean 的 java.util.List 中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆