【问题标题】:Jasper reports List as ParameterJasper 将列表报告为参数
【发布时间】:2015-06-12 06:50:00
【问题描述】:

我在 jasperreport 中作为参数发送渲染列表时遇到问题。我将让 java 应用程序发送填充数据的列表。我希望 jasper 呈现报告。

<?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="r1_10_002_active_agent_invol_jrxml_1275409438288" pageWidth="1040" pageHeight="792" orientation="Landscape" whenNoDataType="NoDataSection" columnWidth="1032" leftMargin="4" rightMargin="4" topMargin="20" bottomMargin="20" uuid="ac26d847-4888-412d-832a-29f36daa0c69">
    <property name="ireport.scriptlethandling" value="0"/>
    <property name="ireport.encoding" value="UTF-8"/>
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <property name="ireport.jasperserver.reportUnit" value="/reports/Test/Test"/>
    <property name="ireport.jasperserver.url" value="http://localhost:8080/jasperserver/services/repository"/>
    <import value="net.sf.jasperreports.engine.*"/>
    <import value="java.util.*"/>
    <import value="net.sf.jasperreports.engine.data.*"/>
    <style name="column_header" mode="Opaque" forecolor="#000000" backcolor="#F1E5D4" hAlign="Center" vAlign="Bottom" fontName="Arial">
        <box bottomPadding="2">
            <pen lineWidth="0.0" lineStyle="Solid" lineColor="#FFFFFF"/>
            <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#FFFFFF"/>
            <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#FFFFFF"/>
            <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#FFFFFF"/>
            <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#FFFFFF"/>
        </box>
    </style>
    <style name="alternate_row" mode="Opaque" forecolor="#000000" backcolor="#FFFFFF" hAlign="Left" vAlign="Middle" fontName="Arial">
        <box leftPadding="2" rightPadding="2">
            <pen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
        </box>
    </style>
    <style name="title_2" isDefault="true" mode="Opaque" forecolor="#000000" backcolor="#FFFFFF" hAlign="Left" vAlign="Middle" fontName="Arial" fontSize="11" isBold="true">
        <box leftPadding="4" rightPadding="2"/>
    </style>
    <style name="default_text" isDefault="true" mode="Opaque" forecolor="#000000" backcolor="#FFFFFF" hAlign="Left" vAlign="Middle" fontName="Arial">
        <box leftPadding="2" rightPadding="2"/>
    </style>
    <style name="column_header_sorted" mode="Opaque" forecolor="#000000" backcolor="#958671" hAlign="Center" vAlign="Bottom" fontName="Arial" fontSize="11" isBold="false">
        <box bottomPadding="2">
            <pen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
        </box>
    </style>
    <style name="title" mode="Opaque" forecolor="#000000" backcolor="#FFFFFF" hAlign="Left" vAlign="Top" fontName="Arial" fontSize="24" isBold="true">
        <box topPadding="2" leftPadding="4"/>
    </style>
    <style name="group_section" mode="Opaque" forecolor="#000000" backcolor="#D3CAB7" hAlign="Left" vAlign="Middle" fontName="Arial" fontSize="12" isBold="false">
        <box leftPadding="2" rightPadding="2">
            <pen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
            <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
        </box>
    </style>
    <style name="group2_section" mode="Opaque" forecolor="#6B542E" backcolor="#FFFFFF" hAlign="Left" vAlign="Middle" fontName="Arial" fontSize="11" isBold="true" isItalic="false">
        <box leftPadding="2" rightPadding="2">
            <pen lineWidth="0.0" lineStyle="Dashed" lineColor="#FFFFFF"/>
            <topPen lineWidth="0.0" lineStyle="Dashed" lineColor="#FFFFFF"/>
            <leftPen lineWidth="0.0" lineStyle="Dashed" lineColor="#FFFFFF"/>
            <bottomPen lineWidth="0.0" lineStyle="Dashed" lineColor="#FFFFFF"/>
            <rightPen lineWidth="0.0" lineStyle="Dashed" lineColor="#FFFFFF"/>
        </box>
    </style>
    <style name="section" mode="Opaque" forecolor="#000000" backcolor="#999999" hAlign="Left" vAlign="Middle" fontName="Arial" fontSize="12">
        <box leftPadding="2" rightPadding="2"/>
    </style>
    <subDataset name="dataset3" uuid="24fb1a54-d444-461e-ac5e-7343f728ee8c">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="company" class="java.lang.String"/>
    </subDataset>
    <parameter name="environment" class="java.lang.String"/>
    <parameter name="username" class="java.lang.String"/>
    <parameter name="show_header" class="java.lang.String"/>
    <parameter name="users" class="java.util.Collection">
        <defaultValueExpression><![CDATA[java.util.Arrays.asList( new vip.User("Agent 1"), new vip.User("Agent 2") )]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[select 1 from dual]]>
    </queryString>
    <field name="id" class="java.lang.Integer"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="58" splitType="Stretch">
            <printWhenExpression><![CDATA[new Boolean($P{show_header}.equals("1"))]]></printWhenExpression>
            <staticText>
                <reportElement uuid="a9d04c94-435d-4baf-9d72-7321d7389aa8" key="staticText-8" style="title" x="194" y="0" width="838" height="33"/>
                <box topPadding="2" leftPadding="4">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement/>
                <text><![CDATA[Active Agent Involuntary Fee]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="c203c163-7092-44d7-86d9-a74572a5c4f8" key="staticText-16" style="title_2" x="194" y="40" width="112" height="15"/>
                <box leftPadding="4" rightPadding="2">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement/>
                <text><![CDATA[Environment: ]]></text>
            </staticText>
            <textField isBlankWhenNull="false">
                <reportElement uuid="e586fc32-d009-4996-97b8-f8931501fe83" key="textField-49" style="default_text" x="306" y="40" width="246" height="15"/>
                <box leftPadding="2" rightPadding="2">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$P{environment}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="e5313ccd-3c5a-4dfd-a2bc-dc9663f6ca16" key="staticText-17" style="title_2" x="552" y="40" width="112" height="15"/>
                <box leftPadding="4" rightPadding="2">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement/>
                <text><![CDATA[User: ]]></text>
            </staticText>
            <textField isBlankWhenNull="false">
                <reportElement uuid="26bb86f6-7bfc-4afc-aee2-6a064197387f" key="textField-50" style="default_text" x="664" y="40" width="251" height="15"/>
                <box leftPadding="2" rightPadding="2">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$P{username}]]></textFieldExpression>
            </textField>
            <image scaleImage="RetainShape">
                <reportElement uuid="e34dbb4b-ca15-4e88-9f91-657e20fc09c7" key="image-1" style="default_text" x="7" y="6" width="177" height="44"/>
                <imageExpression><![CDATA["repo:/PAR/VIPR/Images/Logo_BW.gif"]]></imageExpression>
            </image>
        </band>
    </title>
    <pageHeader>
        <band height="32" splitType="Stretch">
            <textField isBlankWhenNull="false">
                <reportElement uuid="9d71a482-e52e-4fc8-8455-4bc6d834af73" key="textField-3" style="column_header_sorted" x="552" y="0" width="63" height="20"/>
                <box bottomPadding="2">
                    <pen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Center"/>
            </textField>
        </band>
    </pageHeader>
    <detail>
        <band height="200" splitType="Prevent">
            <staticText>
                <reportElement uuid="8d2b2a61-53bc-4d25-be40-e984af9555e5" x="270" y="0" width="200" height="15"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Bean with List sample]]></text>
            </staticText>
            <componentElement>
                <reportElement uuid="c5f0d6ad-400c-4ae8-acdc-92ea0b43a003" x="270" y="0" width="200" height="15"/>
                <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                    <datasetRun subDataset="dataset3" uuid="ec5dd79d-3f64-46d2-b4fa-b6c05abbe86a">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{users})]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="15" width="222">
                        <textField>
                            <reportElement uuid="a9e856e2-9749-4d94-9a46-55384fd1b524" x="100" y="0" width="100" height="15"/>
                            <textElement/>
                            <textFieldExpression><![CDATA[$F{company}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>
        </band>
    </detail>
    <columnFooter>
        <band splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="15" splitType="Stretch">
            <printWhenExpression><![CDATA[new Boolean($P{show_header}.equals("1"))]]></printWhenExpression>
            <textField isBlankWhenNull="false">
                <reportElement uuid="947b6598-b833-41f9-a95b-3496207f0aeb" key="textField-33" style="default_text" x="0" y="0" width="558" height="15"/>
                <box leftPadding="2" rightPadding="2">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA["Date Printed: " + new Date().toLocaleString()]]></textFieldExpression>
            </textField>
            <textField isBlankWhenNull="false">
                <reportElement uuid="6575ad0c-1884-4c8d-adef-ed28e0753d42" key="textField-37" style="default_text" x="558" y="0" width="444" height="15"/>
                <box leftPadding="2" rightPadding="2">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA["Page " + $V{PAGE_NUMBER}.toString() + " of "]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report" isBlankWhenNull="false">
                <reportElement uuid="0d67d248-a477-4c81-8a34-845cb2f7b4e6" key="textField-38" style="default_text" x="1002" y="0" width="30" height="15"/>
                <box leftPadding="2" rightPadding="2">
                    <pen lineWidth="0.0"/>
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
        </band>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>

Java 类:

打包vip;

公共类用户{

private String company;
private String companyType;
private String eecdId;
private String firstName;
private String lastName;
private String email;
private String userId;
private String status;

public User(String company) {
    super();
    this.company = company;
}
public String getEecdId() {
    return eecdId;
}
public void setEecdId(final String eecdId) {
    this.eecdId = eecdId;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(final String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(final String lastName) {
    this.lastName = lastName;
}
public String getEmail() {
    return email;
}
public void setEmail(final String email) {
    this.email = email;
}
public void setCompany(final String company) {
    this.company = company;
}
public String getCompany() {
    return company;
}
public void setCompanyType(final String companyType) {
    this.companyType = companyType;
}
public String getCompanyType() {
    return companyType;
}
public void setUserId(final String userId) {
    this.userId = userId;
}
public String getUserId() {
    return userId;
}
public void setStatus(final String status) {
    this.status = status;
}
public String getStatus() {
    return status;
}

}

但我在尝试访问 JRXML 中的现场公司时遇到错误。

net.sf.jasperreports.engine.JRRuntimeException:net.sf.jasperreports.engine.JRException:从 bean 检索字段值时出错:company    在 net.sf.jasperreports.components.list.VerticalFillList.prepare(VerticalFillList.java: 139)    在 net.sf.jasperreports.engine.fill.JRFillComponentElement.prepare(JRFillComponentElement.java:129)    在 net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:328)     在 net.sf.jasperreports .engine.fill.JRFillBand.fill(JRFillBand.java:377)     在 net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:351)     在 net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand (JRVerticalFiller.java:2039)     在 net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:771)    在 net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:281)     在net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java :144)     在 net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:909)     在 net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:841)     在 net.sf。 jasperreports.engine.fill.JRFiller.fill(JRFiller.java:88)    在 net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:653)     在 net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.fill(JasperFillManager.fill) java:569)     在 net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:915)     在 com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:928)     在 org.openide.util .RequestProcessor$Task.run(RequestProcessor.java:572)     在 org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)  原因:net.sf.jasperreports.engine.JRException:检索字段值时出错bean : 公司     在 net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:123)     在 net.sf。 jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:96)    在 net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:100)    在 net.sf.jasperreports.engine.fill.JRFillDataset。 setOldValues(JRFillDataset.java:1317)     在 net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1218)     在 net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1194)在 net.sf.jasperreports.components.list.FillDatasetRun.next(FillDatasetRun.java:168)     在 net.sf.jasperreports.components.list.VerticalFillList.prepare(VerticalFillList.java:97)     ... 17 更多java.lang.NoSuchMethodException:在 org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1322) 上的“class java.lang.String”类上的未知属性“company”在 org.apache.commons.beanutils.PropertyUtilsBean .getNestedProperty(PropertyUtilsBean.java:770)     在org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:846)    在 org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)    在 net.sf.jasperreports.engine.data.JRAbstractBeanDataSource。 getBeanProperty(JRAbstractBeanDataSource.java:111)     ... 24 更多

Jasper 报告正在努力从 vip.User 中找到该字段。当我使用提示作为 false 使用填充数组列表的 defaultValueExpression 时,它可以工作。

【问题讨论】:

    标签: java jasper-reports


    【解决方案1】:

    为什么需要将列表作为参数发送?

    如无特殊原因。那么应该遵循这个,

    Map parameters = new HashMap();
    JasperPrint jasperPrint = null;
    jasperPrint  = JasperFillManager.fillReport(jasperReport, parameters, new JRBeanCollectionDataSource(yourList));
    

    并且还将您的用户 bean 类添加为 Jrxml 文件中的 Java Beans 数据源。 更多Click

    【讨论】:

    • 我正在使用 jasper 的 WSClient 类。有没有办法发送 JRBeanCollectionDataSource ?谢谢你的帮助。
    • 为什么需要发送列表作为参数?我需要,因为 jasper 没有连接到 LDS 的选项。所以我正在尝试从我的 java 应用程序发送数据。
    • 但是 JRBeanCollectionDataSource 也可以。如果我可以通过 wsclient 发送它。因为我正在调用托管在远程服务器上的 jasper。
    猜你喜欢
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多