"One empty Record" 的错误子数据集
Error Subdataset with "One empty Record"
我想查看所有字段为空但没有来源的报告,所以我必须使用 "One empty Record" 来完成。主要问题是无论我做什么,如果我放入 JRDatasource 表达式的代码中有 JRXmlDataSource,如果我将 "One empty Record" 作为数据源,它会抛出一个错误。
代码如下:
IF($P{REPORT_DATA_SOURCE}.toString().toLowerCase().contains("net.sf.jasperreports.engine.jremptydatasource"),new net.sf.jasperreports.engine.JREmptyDataSource(),((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("SOME XPATH HERE"))
我一直在寻找一整天,但我所做的所有事情都指向同一个点。
Caused by: java.lang.ClassCastException: net.sf.jasperreports.engine.JREmptyDataSource cannot be cast to net.sf.jasperreports.engine.data.JRXmlDataSource
您应该检查 $P{REPORT_DATA_SOURCE}
是不是 net.sf.jasperreports.engine.JREmptyDataSource
的子类。
您的情况下的有效表达式为:
<dataSourceExpression><![CDATA[IF(($P{REPORT_DATA_SOURCE} instanceof net.sf.jasperreports.engine.JREmptyDataSource), new net.sf.jasperreports.engine.JREmptyDataSource(), ((net.sf.jasperreports.engine.data.JRXmlDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("SOME XPATH HERE"))]]></dataSourceExpression>
例如,此 Java 代码将与下面的 jrxml 一起工作:
Map<String, Object> params = new HashMap<>();
//params.put("REPORT_DATA_SOURCE", new JRXmlDataSource("customers.xml", "/Customers/*")); // this is working expression also
params.put("REPORT_DATA_SOURCE", new JREmptyDataSource(1)); // one record dataset
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
模板:
<?xml version="1.0" encoding="UTF-8"?>
<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="Report with check of datasource" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<parameter name="REPORT_DATASOURCE" class="java.lang.Object"/>
<field name="id" class="java.lang.String">
<fieldDescription><![CDATA[child::text()]]></fieldDescription>
</field>
<variable name="expression" class="java.lang.String">
<variableExpression><![CDATA["/Customers/Customer[CustomerID='" + $F{id} + "']"]]></variableExpression>
</variable>
<detail>
<band height="70" splitType="Stretch">
<textField>
<reportElement x="100" y="0" width="100" height="15"/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="0" width="100" height="15"/>
<text><![CDATA[Id:]]></text>
</staticText>
<subreport>
<reportElement x="54" y="15" width="380" height="45"/>
<dataSourceExpression><![CDATA[IF(($P{REPORT_DATA_SOURCE} instanceof net.sf.jasperreports.engine.JREmptyDataSource), new net.sf.jasperreports.engine.JREmptyDataSource(), ((net.sf.jasperreports.engine.data.JRXmlDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("SOME XPATH HERE"))]]></dataSourceExpression>
<subreportExpression><![CDATA["./jrxml/subreport_with_xml.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
我想查看所有字段为空但没有来源的报告,所以我必须使用 "One empty Record" 来完成。主要问题是无论我做什么,如果我放入 JRDatasource 表达式的代码中有 JRXmlDataSource,如果我将 "One empty Record" 作为数据源,它会抛出一个错误。
代码如下:
IF($P{REPORT_DATA_SOURCE}.toString().toLowerCase().contains("net.sf.jasperreports.engine.jremptydatasource"),new net.sf.jasperreports.engine.JREmptyDataSource(),((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("SOME XPATH HERE"))
我一直在寻找一整天,但我所做的所有事情都指向同一个点。
Caused by: java.lang.ClassCastException: net.sf.jasperreports.engine.JREmptyDataSource cannot be cast to net.sf.jasperreports.engine.data.JRXmlDataSource
您应该检查 $P{REPORT_DATA_SOURCE}
是不是 net.sf.jasperreports.engine.JREmptyDataSource
的子类。
您的情况下的有效表达式为:
<dataSourceExpression><![CDATA[IF(($P{REPORT_DATA_SOURCE} instanceof net.sf.jasperreports.engine.JREmptyDataSource), new net.sf.jasperreports.engine.JREmptyDataSource(), ((net.sf.jasperreports.engine.data.JRXmlDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("SOME XPATH HERE"))]]></dataSourceExpression>
例如,此 Java 代码将与下面的 jrxml 一起工作:
Map<String, Object> params = new HashMap<>();
//params.put("REPORT_DATA_SOURCE", new JRXmlDataSource("customers.xml", "/Customers/*")); // this is working expression also
params.put("REPORT_DATA_SOURCE", new JREmptyDataSource(1)); // one record dataset
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
模板:
<?xml version="1.0" encoding="UTF-8"?>
<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="Report with check of datasource" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<parameter name="REPORT_DATASOURCE" class="java.lang.Object"/>
<field name="id" class="java.lang.String">
<fieldDescription><![CDATA[child::text()]]></fieldDescription>
</field>
<variable name="expression" class="java.lang.String">
<variableExpression><![CDATA["/Customers/Customer[CustomerID='" + $F{id} + "']"]]></variableExpression>
</variable>
<detail>
<band height="70" splitType="Stretch">
<textField>
<reportElement x="100" y="0" width="100" height="15"/>
<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="0" width="100" height="15"/>
<text><![CDATA[Id:]]></text>
</staticText>
<subreport>
<reportElement x="54" y="15" width="380" height="45"/>
<dataSourceExpression><![CDATA[IF(($P{REPORT_DATA_SOURCE} instanceof net.sf.jasperreports.engine.JREmptyDataSource), new net.sf.jasperreports.engine.JREmptyDataSource(), ((net.sf.jasperreports.engine.data.JRXmlDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("SOME XPATH HERE"))]]></dataSourceExpression>
<subreportExpression><![CDATA["./jrxml/subreport_with_xml.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>