CustomerAddressDataSource

CustomerAddressDataSource

本文介绍了在Jaspersoft Studio中,主报告未显示Java Bean的基本子报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Jaspersoft Studio中使用JavaBean生成基本报告(主/子报告)时遇到麻烦.

I'm having trouble generating a basic report (master/subreport) with JavaBeans in Jaspersoft Studio.

我创建了 TestMainReport.jrxml TestSubreport.jrxml .

TestMainReport.jrxml 包含两个静态文本字段,在标题栏中标记为"A Title",在摘要栏中标记为"A Summary".

TestMainReport.jrxml contains two static text fields, labeled "A Title" in the title band and "A Summary" in the summary band.

TestSubreport.jrxml 包含两个静态文本字段,标题为"Subreport Title",摘要区域为"Subreport Summary".

TestSubreport.jrxml contains two static text fields, "Subreport Title" in the title and "Subreport Summary" in the summary band.

我已经为它们分配了未使用的JavaBeans数据适配器(尽管JavaBean字段正在主报表中映射.由于没有使用它们,我碰巧没有在子报表中映射它们).

I've assigned JavaBeans Data Adapters to them, which are not used (although the JavaBean fields are being mapped in the master report. I just happened to not map them in the subreport since they are not used).

子报表元素已添加到摘要"区域中的主报表中.

A subreport element has been added to the master report in the Summary band.

当我尝试生成每个报告时,这两个报告都是单独生成的.但是,子报表静态文本不会出现在主报表中.

Both reports are well generated individually when I try to generate each one. However, the subreport static texts won't appear in the master report.

我期望子报表的静态文本会出现在主报表中.

I was expecting that the subreport's static texts would appear in the master report.

我在做什么错了?

TestMainReport.jrxml

TestMainReport.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="TestMainReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0d969cfb-66d2-442f-b7a4-5a9e1a40c3ae">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Customer Info Data Adapter"/>
    <field name="birthday" class="java.time.LocalDate">
        <fieldDescription><![CDATA[birthday]]></fieldDescription>
    </field>
    <field name="observacao" class="java.lang.String">
        <fieldDescription><![CDATA[observacao]]></fieldDescription>
    </field>
    <field name="orderNumber" class="java.lang.Integer">
        <fieldDescription><![CDATA[orderNumber]]></fieldDescription>
    </field>
    <field name="phone" class="java.lang.String">
        <fieldDescription><![CDATA[phone]]></fieldDescription>
    </field>
    <field name="name" class="java.lang.String">
        <fieldDescription><![CDATA[name]]></fieldDescription>
    </field>
    <field name="email" class="java.lang.String">
        <fieldDescription><![CDATA[email]]></fieldDescription>
    </field>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="30" uuid="db07ac65-15f6-4190-b1db-9d445456f306"/>
                <text><![CDATA[A Title]]></text>
            </staticText>
        </band>
    </title>
    <summary>
        <band height="215" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="30" uuid="08c03e87-2b15-4eb1-b404-b7dce6dfb890"/>
                <text><![CDATA[A Summary]]></text>
            </staticText>
            <subreport>
                <reportElement x="0" y="30" width="560" height="150" uuid="c292246e-1ffa-4f08-a783-a0b05b28be76"/>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["TestSubreport.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </summary>
</jasperReport>

TestSubreport.jrxml

TestSubreport.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="TestSubreport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d5dd9821-786d-4312-81c9-fd77f1abfb8a">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Customer Addresses Data Adapter"/>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="30" uuid="4c9fdc83-4039-4eed-b593-448898853071"/>
                <text><![CDATA[Subreport Title]]></text>
            </staticText>
        </band>
    </title>
    <summary>
        <band height="42" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="30" uuid="4bb9ba45-548a-4e87-a543-472b0f960487"/>
                <text><![CDATA[Subreport Summary]]></text>
            </staticText>
        </band>
    </summary>
</jasperReport>

CustomerInfoDataSource.java

CustomerInfoDataSource.java

package testdatasource;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class CustomerInfoDataSource {

    public static Collection<CustomerInfo> getCustomerInfo() {
        List<CustomerInfo> info = new ArrayList<>();
        info.add(new CustomerInfo(1, "Mario", "[email protected]", LocalDate.now(), "14 912345678", "Observação Mario"));
        return info;
    }
}

CustomerInfo.java

CustomerInfo.java

package testdatasource;

import java.time.LocalDate;

public class CustomerInfo {

    private final int orderNumber;
    private final String name;
    private final String email;
    private final LocalDate birthday;
    private final String phone;
    private final String observacao;

    public CustomerInfo(int orderNumber, String name, String email, LocalDate birthday, String phone, String observacao) {
        this.orderNumber = orderNumber;
        this.name = name;
        this.email = email;
        this.birthday = birthday;
        this.phone = phone;
        this.observacao = observacao;
    }

    public int getOrderNumber() {
        return orderNumber;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    public LocalDate getBirthday() {
        return birthday;
    }

    public String getPhone() {
        return phone;
    }

    public String getObservacao() {
        return observacao;
    }
}

CustomerAddressDataSource.java

CustomerAddressDataSource.java

package testdatasource;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class CustomerAddressDataSource {

    public static Collection<CustomerAddress> getCustomerAddresses() {
        List<CustomerAddress> addresses = new ArrayList<>();
        addresses.add(new CustomerAddress("Casa 1", "Rua Tal", "123", null, "Jardim Márcia", "Agudos", "17400-000", "Perto da caixa d'água"));
        addresses.add(new CustomerAddress("Casa 2", "Rua Tal", "456", null, "Jardim Márcia", "Agudos", "17400-000", "Perto da caixa d'água"));
        return addresses;
    }
}

CustomerAddress.java

CustomerAddress.java

package testdatasource;

public class CustomerAddress {

    private final String title;
    private final String street;
    private final String number;
    private final String complement;
    private final String bairro;
    private final String city;
    private final String cep;
    private final String referencePoint;

    public CustomerAddress(String title, String street, String number, String complement, String bairro, String city,
            String cep, String referencePoint) {
        this.title = title;
        this.street = street;
        this.number = number;
        this.complement = complement;
        this.bairro = bairro;
        this.city = city;
        this.cep = cep;
        this.referencePoint = referencePoint;
    }

    public String getTitle() {
        return title;
    }

    public String getStreet() {
        return street;
    }

    public String getNumber() {
        return number;
    }

    public String getComplement() {
        return complement;
    }

    public String getBairro() {
        return bairro;
    }

    public String getCity() {
        return city;
    }

    public String getCep() {
        return cep;
    }

    public String getReferencePoint() {
        return referencePoint;
    }
}

TestMainReport.jrxml的输出:

TestSubreport.jrxml的输出:

推荐答案

出了什么问题?

您没有为子报表指定dataSource.取而代之的是设置 connectionExpression (<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>).该连接可以在基于 jdbc 的数据源(报告)的情况下为您提供帮助,但在您的情况下则不会.

What is wrong?

You did not specify dataSource for subreport. Instead of this you have set connectionExpression (<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>). The connection can help you in case jdbc based datasources (reports), but not at your case.

您应该为子报表指定数据源.您可以这样声明subreport元素:

You should specify datasource for subreport. You can declare subreport element like this:

<subreport>
    <reportElement x="0" y="30" width="560" height="150"/>
    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(testdatasource.CustomerAddressDataSource.getCustomerAddresses())]]></dataSourceExpression>
    <subreportExpression><![CDATA["TestSubreport.jasper"]]></subreportExpression>
</subreport>

在这种情况下, CustomerAddressDataSource 类将用于填充子报表.

In this case CustomerAddressDataSource class will be used for filling subreport.

您可以将子报表的数据适配器导出到文件中,并将其保存到与模板相同的文件夹中(如果您不想指定数据适配器文件的路径).

You can export Data Adapter of subreport to the file and save it to the same folder as templates (if you don't want to specify path to data adapter file).

您可以在 JSS ( Jaspersoft Studio )的上下文菜单中导出 Data Adapter 定义:

You can export Data Adapter definition with help of context menu at JSS (Jaspersoft Studio):

以您的情况为准-内容为 CustomerAddressesDataAdapter.xml :

In your case - it will be CustomerAddressesDataAdapter.xml with this content:

<?xml version="1.0" encoding="UTF-8" ?>
<beanDataAdapter class="net.sf.jasperreports.data.bean.BeanDataAdapterImpl"><name>Customer Addresses Data Adapter</name><factoryClass>testdatasource.CustomerAddressDataSource</factoryClass><methodName>getCustomerAddresses</methodName><useFieldDescription>false</useFieldDescription><classpath>C:\somepath\library_with_beans.jar</classpath></beanDataAdapter>

您应该在 net.sf.jasperreports.data.adapter 属性.借助此属性,您应该使用 Data Adapter 定义(描述)指定文件名,它可以是带有路径的名称.就我而言,应该是:CustomerAddressesDataAdapter.xml.

You should specify this adapter at subreport with help of net.sf.jasperreports.data.adapter property. With help of this property you should specify name of file with Data Adapter definition (description), it can be name with path. At my case it will be: CustomerAddressesDataAdapter.xml.

子报表模板为:

<?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="TestSubreport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="net.sf.jasperreports.data.adapter" value="CustomerAddressesDataAdapter.xml"/>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="30"/>
                <text><![CDATA[Subreport Title]]></text>
            </staticText>
        </band>
    </title>
    <summary>
        <band height="42" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="0" width="100" height="30"/>
                <text><![CDATA[Subreport Summary]]></text>
            </staticText>
        </band>
    </summary>
</jasperReport>

在主报告中,带有子报告的部分将是:

At master report the section with subreport will be:

<subreport>
    <reportElement x="0" y="30" width="560" height="150"/>
    <subreportExpression><![CDATA["TestSubreport.jasper"]]></subreportExpression>
</subreport>

-与以前的情况一样,您无需指定连接.

- as at previous case you don't need to specify connection.

对于第一个解决方案, JSS 的输出结果将是:

As for the first solution the output result at JSS will be:

07-31 13:14