如何修复传递JavaBean作为报告参数的异常?

时间:2018-01-12 14:39:36

标签: java jasper-reports

我需要放一些复杂的(任意的,pojo)对象来报告。我尝试使用parameter标记用于此目的,但它不起作用。我尝试谷歌这个,但我创建了简单的参数类型(如String或Long)或使用一些数据源的例子,但我认为我不需要它只为1个对象。

我的pojo是:

public class CaseDescription {

private final Long id;
private final String logoPath;
private final String userName;
private final String location;

public CaseDescription(final Long id, final String logoPath, final String userName, final String location) {
    this.id = id;
    this.logoPath = logoPath;
    this.userName = userName;
    this.location = location;
}

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

public String getLogoPath() {
    return this.logoPath;
}

public String getUserName() {
    return this.userName;
}

public String getLocation() {
    return this.location;
}

}

和我的jrxml看起来像:

<?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="cool_name" pageWidth="595" pageHeight="842" orientation="Landscape" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="13acf49e-4913-4d1b-bccc-113817ad15d1">
...
<parameter name="caseDescription" class="path.to.case.description.package.CaseDescription" isForPrompting="false"/>
...
<group name="Group1">
    ...
    <textField>
        <reportElement style="SubTitle" stretchType="ContainerBottom" x="0" y="30" width="451" height="29" forecolor="#000000" uuid="4636f2b9-3394-42cd-ac90-e34f582b2272"/>
        <textElement textAlignment="Left">
            <font size="22" isBold="false"/>
        </textElement>
        <textFieldExpression><![CDATA["Accused : " + $P{caseDescription.userName}]]></textFieldExpression>
    </textField>
    ...
</group>
...
</jasperReport>

我尝试通过以下代码生成文档:

final CaseDescription caseDescription = new CaseDescription(1L, "Some", "Gump", "Forrest");
Map<String, Object> parameters = new HashMap<>();
parameters.put("caseDescription", caseDescription);
JasperDesign jasperDesign = JRXmlLoader.load(resourceAsStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint, pathForSaving);

我得到了例外:

net.sf.jasperreports.engine.design.JRValidationException: Report design not valid : 
 1. Parameter not found : caseDescription.userName
at net.sf.jasperreports.engine.design.JRAbstractCompiler.verifyDesign(JRAbstractCompiler.java:280)
at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:152)
at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:357)
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:617)

但如果我将caseDescription.username替换为caseDescription而不是将caseDescription.toString()替换为此字段。像caseDescription.getUserName()这样的事情也不起作用。谁能解释一下我做错了什么?

1 个答案:

答案 0 :(得分:0)

您应该考虑 JasperReports 的参数,例如 Java 对象。

您需要调用对象(类)的方法。在您的情况下,您需要拨打公共getter CaseDescription.getUserName()

您可以像这样更改表达式:

<textField>
    <reportElement x="0" y="30" width="451" height="29"/>
    <textFieldExpression><![CDATA["Accused : " + $P{caseDescription}.getUserName()]]></textFieldExpression>
</textField>

或者像这样

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" ...>
...
    <import value="your.package.name.CaseDescription"/>
...
    <parameter name="caseDescription" class="java.lang.Object"/>
...
    <textField>
        <reportElement x="0" y="30" width="451" height="29"/>
        <textFieldExpression><![CDATA["Accused : " + ((CaseDescription) $P{caseDescription}).getUserName()]]></textFieldExpression>
    </textField>
...
</jasperReport>

注意:

为了能够使用类的简单名称(不是完全限定名称),您需要在 jrxml 文件中添加<import value="your.package.name.CaseDescription"/>