Search code examples
javajasper-reports

How to iterate HashMap(Arraylist) in jrxml?


I am trying to iterate the values of the HashMap passed as parameters to jrxml, in the HashMap the values are passed as List.

Example

List<String> severity = // some values;
List<String> messages = // some values;

LinkedHashMap parameters = new LinkedHashMap();
parameters.put("severity", severity);
parameters.put("messages", messages);

jPrint = JasperFillManager.fillReport(jasperreport,parameters,new JREmptyDataSource());     
JasperExportManager.exportReportToPdfStream(jPrint,baos);  

How can I iterate these List inside jasper report the jrxml?


Solution

  • You can create a new datasource with jasper report using

    new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{severity})
    

    This datasource can be used for subreport as dataSourceExpression:

    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{severity})]]></dataSourceExpression>
    

    or when using subDataset in jr:table, jr:list components

    <datasetRun subDataset="listDs" uuid="99b1de41-5185-4b8f-9cfe-011e7b4cf22d">
       <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{severity})]]></dataSourceExpression>
    </datasetRun>
    

    Be aware since your Bean is a java primitive type (String) to access it directly you need to use this field description

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

    See this for more information: Passing the List of primitive type objects as datasource for subreport