Passing the List of primitive type objects as datasource for subreport
You have to specify what field you're using in your sub report. You are passing $F{}
and as you are passing a List<String>
as a DataSource you should put $F{_THIS}
. Of course you have to add a field with that name too, only doing that you can use the expression $F{somefield}
.
You can use this datasource expression for passing java.util.List (via parameter) to subreport:
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{seznamPriloh})]]></dataSourceExpression>
The working sample, master report:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
<parameter name="listParam" class="java.util.List"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["<subreport_dir>"]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[SELECT id, street, city FROM address]]>
</queryString>
<field name="ID" class="java.lang.Integer"/>
<field name="STREET" class="java.lang.String"/>
<field name="CITY" class="java.lang.String"/>
<detail>
<band height="57" splitType="Stretch">
<frame>
<reportElement x="0" y="0" width="539" height="57"/>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<subreport>
<reportElement x="0" y="32" width="523" height="17"/>
<subreportParameter name="cityParam">
<subreportParameterExpression><![CDATA[$F{CITY}]]></subreportParameterExpression>
</subreportParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{listParam})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "subreport_list_as_param.jasper"]]></subreportExpression>
</subreport>
<textField>
<reportElement x="300" y="0" width="208" height="20"/>
<box leftPadding="10"/>
<textElement>
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["City: " + $F{CITY}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="200" height="20"/>
<box leftPadding="10"/>
<textElement>
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["Street: " + $F{STREET}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement>
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["Id: " + $F{ID}]]></textFieldExpression>
</textField>
</frame>
</band>
</detail>
</jasperReport>
The subreport:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
<parameter name="cityParam" class="java.lang.String"/>
<field name="id" class="java.lang.Integer"/>
<field name="station" class="java.lang.String"/>
<field name="city" class="java.lang.String"/>
<filterExpression><![CDATA[$F{city}.equals($P{cityParam})]]></filterExpression>
<title>
<band height="39">
<textField>
<reportElement x="220" y="14" width="161" height="20"/>
<box leftPadding="10"/>
<textElement>
<font isBold="true" isItalic="true"/>
</textElement>
<textFieldExpression><![CDATA["City param: " + $P{cityParam}]]></textFieldExpression>
</textField>
</band>
</title>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20"/>
<box leftPadding="10"/>
<textElement/>
<textFieldExpression><![CDATA[$F{station}]]></textFieldExpression>
</textField>
</band>
</detail>
<noData>
<band height="50">
<textField>
<reportElement x="220" y="17" width="161" height="20"/>
<box leftPadding="10"/>
<textElement>
<font isBold="true" isItalic="true"/>
</textElement>
<textFieldExpression><![CDATA["No data for city param: " + $P{cityParam}]]></textFieldExpression>
</textField>
</band>
</noData>
</jasperReport>
The Java code for passing List:
Map<String, Object> params = new HashMap<String, Object>();
List<TestBean> beansList = new ArrayList<TestBean>();
// The TestBean class constructor is:
//public TestBean(String city, Integer id, String station)
TestBean bean = new TestBean("Dallas", 10, "Central park st.");
beansList.add(bean);
bean = new TestBean("Dallas", 11, "Railway st.");
beansList.add(bean);
bean = new TestBean("Dallas", 12, "Market st.");
beansList.add(bean);
bean = new TestBean("Lyon", 20, "Airport st.");
beansList.add(bean);
params.put("listParam", beansList);
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, getDemoHsqldbConnection());
JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
The result will be (view of generated PDF file):
You can look at the implementations of net.sf.jasperreports.engine.JRDataSource. The most appropriate for your case are: JRBeanCollectionDataSource and JRBeanArrayDataSource. As you can see they are both Bean-based.
I think you can easily convert your List<String>
to the List<StringBean>
.
Or you can implement your own JRDataSource.