30

My JavaBean contains the java.util.List.

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

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

1

3 Answers 3

55

Here is the working sample.

The key points of this sample:

  • using of the _THIS expression;
  • using List (jr:list) component in Detail band

The snippet of code for generating report:

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);
}

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;
    }
}

The jrxml file:

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

The result will be:

The resulting report in PDF format


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.

7
  • 1
    you have list of cities but in the <textFieldExpression><![CDATA[$F{city}]]></textFieldExpression></textField> you take the field "city". it comes from where?,please explain.
    – Girish K
    Commented Feb 3, 2014 at 13:03
  • 2
    @GirishK Look at field's declaration in jrxml file
    – Alex K
    Commented Feb 3, 2014 at 14:38
  • You used 'cities' as the field name for the collection despite the actual name of the list inside BeanWithList is 'm_cities', also there is no reference for 'cities' as in 'subDataset' element. How does it actually work? Commented Jun 10, 2017 at 12:48
  • 2
    @MichaelAssraf The public getter is public List<String> getCities(). It means that you can use cities name
    – Alex K
    Commented Jun 10, 2017 at 13:03
  • I switch it to this syntax in last edit, to make it more clear, feel free to roll it back if you don't like it. Commented Oct 11, 2018 at 19:33
0
public void generisiIzvestaj(HttpServletRequest request, HttpServletResponse response) throws Exception { 
        
        List<Predstava> predstave = (List<Predstava>)request.getSession().getAttribute("predstaveR");
    
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(predstave);
        InputStream inputStream = this.getClass().getResourceAsStream("/reports/Predstave.jrxml");
        JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
        Map<String, Object> params = new HashMap<String, Object>();
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
        inputStream.close();
        
        response.setContentType("application/x-download");
        response.addHeader("Content-disposition", "attachment; filename=PredstaveRezisera.pdf");
        ServletOutputStream out = response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint,out);
0

The code marked as solution didn't work for me...

I had net.sf.jasperreports.engine.JRException: Error retrieving field value from bean: id when testing it.

I have solved the issue by creating another subDataset for cities property, then created _THIS field "inside" this subDataset.

Here's the jrxm file:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.20.6.final using JasperReports Library version 6.20.6-5c96b6aa8a39ac1dc6b6bea4b81168e16dd39231  -->
<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="teste" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fd086588-2253-40dd-9c2e-4d74d88ce758">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="example"/>
    <subDataset name="dataset1" uuid="a7665780-5506-4544-b341-6924d91bb9a4">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="example"/>
        <field name="cities" class="java.util.List">
            <fieldDescription><![CDATA[cities]]></fieldDescription>
        </field>
        <field name="id" class="java.lang.Integer">
            <fieldDescription><![CDATA[id]]></fieldDescription>
        </field>
    </subDataset>
    <subDataset name="dataset2" uuid="a521cb83-2843-44ae-8491-3eb876e2c88b">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="example"/>
        <field name="_THIS" class="java.lang.String"/>
    </subDataset>
    <field name="cities" class="java.util.List">
        <fieldDescription><![CDATA[cities]]></fieldDescription>
    </field>
    <field name="id" class="java.lang.Integer">
        <fieldDescription><![CDATA[id]]></fieldDescription>
    </field>
    <title>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement x="140" y="0" width="258" height="20" uuid="cb84e20c-6cec-4134-aee9-5dc2f5ad4724"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Bean with List sample]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="40">
            <staticText>
                <reportElement x="0" y="20" width="80" height="20" uuid="f5c562a0-bfe1-48a1-bb2f-71aacce28f00">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="fdc8b78c-7bdc-44f9-9531-9671b61a3dfb"/>
                </reportElement>
                <text><![CDATA[id]]></text>
            </staticText>
            <staticText>
                <reportElement x="80" y="20" width="200" height="20" uuid="087dd74d-bc46-4971-a89f-213d31585c7d">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="34dc9ae4-c625-4264-a732-c72fe5321a3b"/>
                </reportElement>
                <text><![CDATA[cities]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="80" height="30" uuid="0a056819-8e28-406d-928b-9b607372cad0">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="c16fd7ea-2015-4c57-afdc-61db5bda93ed"/>
                </reportElement>
                <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
            </textField>
            <componentElement>
                <reportElement x="80" y="0" width="116" height="30" uuid="3263bb61-d468-4e25-b1e7-c15bfdddd151"/>
                <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="dataset2" uuid="1b1fdd6f-b6e7-415e-996e-8e8f11251529">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{cities})]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="30" width="116">
                        <textField>
                            <reportElement x="0" y="0" width="100" height="30" uuid="23434b69-96f9-4464-8c32-d43f6ffcdd8e"/>
                            <textFieldExpression><![CDATA[$F{_THIS}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>
        </band>
    </detail>
</jasperReport>

Not the answer you're looking for? Browse other questions tagged or ask your own question.