在JasperReport中从bean中检索字段值时出错

时间:2015-12-18 19:26:41

标签: java jasper-reports

以下是背景信息:我有一个使用JasperReport和iReport构建的报告。在其中我试图在scriptlet的帮助下构建索引。这是scriptlet的代码。

public class ToCScriptlet extends JRDefaultScriptlet {

public String test(){
    return "WUBBA LUBBA DUB DUB!";
}

private static ArrayList<String> tableOfContentNames=new ArrayList<String>();
public static ArrayList<HeadingBean> headers=new ArrayList<HeadingBean>();

//  WE RETURN AN EMPTY STRING, SINCE WE INVOKE THE FUNCTION FROM A TEXT FIELD
//  THIS WILL MAKE SURE THE FIELD IS EMPTY, INSTEAD OF SHOWING "null"
public String addIndexName(String name){
    tableOfContentNames.add(name);
    return name+" added";
}

public String addHeader(String name,int pageNumber){
    headers.add(new HeadingBean(name, pageNumber));
    String result="";
    for(HeadingBean header:headers){
        result+=header.getHeadingText()+" "+header.getPageIndex()+" / ";
    }
    return result;
}
}

使用它,我收集应该在那里的每个部分的索引名称和页码。这些信息收集在这些bean中:

public class HeadingBean {

private String headingText;
private Integer pageIndex;

public HeadingBean(String text, Integer pageIndex) {
    this.headingText = text;
    this.pageIndex = pageIndex;
}

public String getHeadingText() {
    return this.headingText;
}

public Integer getPageIndex() {
    return this.pageIndex;
}

public void setHeadingText(String headingText){
    this.headingText=headingText;
}

public void setPageIndex(Integer pageIndex){
    this.pageIndex=pageIndex;
}

}

索引位于最后一个详细信息区域的子报告中,该子报告接收使用scriptlet中的ArrayList创建的数据源:

<subreport isUsingCache="false">
            <reportElement x="0" y="1" width="595" height="25" uuid="2857c139-c43e-4180-bb0b-4f4fdd39dd91"/>
            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanArrayDataSource($P{REPORT_SCRIPTLET}.getHeaders())]]></dataSourceExpression>
            <subreportExpression><![CDATA["subreport_table_of_content.jasper"]]></subreportExpression>
</subreport>

带索引的子报告是这样的:

<?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_transactions_subreport_platform_health" language="groovy" pageWidth="595" pageHeight="572" orientation="Landscape" whenNoDataType="AllSectionsNoDetail" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" scriptletClass="userFunctions.ToCScriptlet" resourceBundle="JKpiReportLocalization" uuid="09b0093c-983f-49f8-bc81-7baf861acff5">
<property name="ireport.zoom" value="1.331000000000001"/>
<property name="ireport.x" value="30"/>
<property name="ireport.y" value="0"/>
<template><![CDATA["report_style.jrtx"]]></template>
<scriptlet name="UTILITIES" class="userFunctions.UtilityFunctions"/>
<parameter name="NOT_IN_USE" class="java.lang.Number">
    <defaultValueExpression><![CDATA[0]]></defaultValueExpression>
</parameter>
<queryString>
    <![CDATA[]]>
</queryString>
<field name="headingText" class="java.lang.String">
    <fieldDescription><![CDATA[headingText]]></fieldDescription>
</field>
<field name="pageIndex" class="java.lang.Integer">
    <fieldDescription><![CDATA[pageIndex]]></fieldDescription>
</field>
<variable name="CURRENCY" class="java.lang.String">
    <variableExpression><![CDATA[]]></variableExpression>
    <initialValueExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.get("CURRENCY").hasParameter("NAME") ? $P{REPORT_PARAMETERS_MAP}.get("CURRENCY").getParameter("NAME").toString() : ""]]></initialValueExpression>
</variable>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="65" splitType="Stretch">
        <rectangle radius="0">
            <reportElement style="Subreport Section Title Background" mode="Opaque" x="0" y="10" width="595" height="30" uuid="abb4c5e4-ee6a-4620-9006-3b28cf3dc559"/>
        </rectangle>
        <textField evaluationTime="Report">
            <reportElement style="Subreport Title" x="0" y="0" width="595" height="50" uuid="e9158915-e6ef-40c4-804d-5ad272af9d5d"/>
            <textElement textAlignment="Center" verticalAlignment="Middle" markup="none">
                <font size="16"/>
            </textElement>
            <textFieldExpression><![CDATA[str("report.subreport.tableOfContent.title")]]></textFieldExpression>
        </textField>
    </band>
</title>
<columnHeader>
    <band height="21">
        <textField evaluationTime="Report">
            <reportElement x="83" y="0" width="100" height="20" uuid="fa959a40-465c-443d-8bad-bb4a8d449f29"/>
            <textFieldExpression><![CDATA[str("report.subreport.tableOfContent.name")]]></textFieldExpression>
        </textField>
        <textField evaluationTime="Report">
            <reportElement x="401" y="1" width="100" height="20" uuid="61f77907-24fd-49cb-a5a5-48f02a9214c3"/>
            <textFieldExpression><![CDATA[str("report.subreport.tableOfContent.page")]]></textFieldExpression>
        </textField>
    </band>
</columnHeader>
<detail>
    <band height="21">
        <textField evaluationTime="Report">
            <reportElement x="83" y="0" width="318" height="20" uuid="b9f98ed4-24af-4c99-a753-044cae79a6ed"/>
            <textFieldExpression><![CDATA[$F{headingText}]]></textFieldExpression>
        </textField>
        <textField evaluationTime="Report">
            <reportElement x="401" y="0" width="100" height="20" uuid="56eaf6f4-d1fb-49e9-bd2e-cfc06f208a4d"/>
            <textFieldExpression><![CDATA[$F{pageIndex}]]></textFieldExpression>
        </textField>
    </band>
</detail>
<summary>
    <band height="81">
        <textField evaluationTime="Report">
            <reportElement x="0" y="0" width="595" height="20" uuid="25ff1c35-b0b8-4656-a01c-6603efa923a7"/>
            <textFieldExpression><![CDATA[$P{REPORT_SCRIPTLET}.test()]]></textFieldExpression>
        </textField>
        <textField evaluationTime="Report">
            <reportElement x="0" y="20" width="595" height="20" uuid="98c3cd2b-df00-4c28-aaec-549d8dd1797f"/>
            <textFieldExpression><![CDATA[$P{REPORT_SCRIPTLET}.getTableOfContentNames()]]></textFieldExpression>
        </textField>
        <textField evaluationTime="Report">
            <reportElement x="0" y="40" width="595" height="20" uuid="5e6841ae-163b-4d3d-ae37-4f21f5440751"/>
            <textFieldExpression><![CDATA[$P{REPORT_SCRIPTLET}.getHeaders()]]></textFieldExpression>
        </textField>
    </band>
</summary>
</jasperReport>

我四处寻找可能导致问题的原因,但我找不到任何东西。我检查过bean是公开的,名字是正确的,但我找不到问题所在。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

发现问题。我曾经在iReport中直接创建JRArrayDataSource,使用数组作为参数,它会产生错误。我尝试在scriptlet中创建一个函数来构建并返回de DataSource,然后我注意到有两个实例化函数,我正在使用的常用函数和一个带布尔值的函数。我尝试在布尔值中传递“true”,使用数组,现在可以正常工作。