Jasper reports 主报告不显示Jaspersoft Studio中包含Java bean的基本子报告

Jasper reports 主报告不显示Jaspersoft Studio中包含Java bean的基本子报告,jasper-reports,javabeans,jaspersoft-studio,Jasper Reports,Javabeans,Jaspersoft Studio,我在Jaspersoft Studio中使用JavaBeans生成基本报告(主/子报告)时遇到问题 我创建了TestMainReport.jrxml和TestSubreport.jrxml TestMainReport.jrxml包含两个静态文本字段,在标题栏中标记为“标题”,在摘要栏中标记为“摘要” TestSubreport.jrxml包含两个静态文本字段,标题中的“Subreport Title”,以及摘要栏中的“Subreport Summary” 我已经为它们分配了JavaBeans数

我在Jaspersoft Studio中使用JavaBeans生成基本报告(主/子报告)时遇到问题

我创建了TestMainReport.jrxml和TestSubreport.jrxml

TestMainReport.jrxml包含两个静态文本字段,在标题栏中标记为
“标题”
,在摘要栏中标记为
“摘要”

TestSubreport.jrxml包含两个静态文本字段,
标题中的“Subreport Title”
,以及摘要栏中的“Subreport Summary”

我已经为它们分配了JavaBeans数据适配器,这些适配器没有被使用(尽管JavaBean字段在主报表中被映射。我只是碰巧没有在子报表中映射它们,因为它们没有被使用)

已将子报表元素添加到摘要栏中的主报表中

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

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

我做错了什么

testmainleport.jrxml

CustomerAddressDataSource.java

TestMainReport.jrxml的输出:

TestSubreport.jrxml的输出:

怎么了? 您没有为子报表指定数据源。相反,您设置了connectionExpression(
)。在基于jdbc的数据源(报告)的情况下,该连接可以帮助您,但在您的情况下则不行

解决方案1-使用dataSourceExpression 您应该为子报表指定数据源。您可以这样声明子报表元素:

<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>
在主报告中,包含子报告的部分将为:

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

-与前一种情况一样,您不需要指定连接

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


更多信息

@AlexK对不起,我是说2个静态文本。谢谢!我不知道数据源需要这样设置。我自己无法在在线文档中找到解决方案--您是否有关于如何在搜索中找到它的提示?欢迎:)
关于如何在搜索中找到它的提示?
-如果您知道要找到什么,很难回答-您有优势:)。我认为搜索JR的好工作流程是:“查看JasperReports终极指南”->“查看示例参考()”->“查看JSS帮助”->“搜索SO/community.jaspersoft.com”。我在我的帖子中添加了一些有用的链接。
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", "mario@mario.com.br", LocalDate.now(), "14 912345678", "Observação Mario"));
        return info;
    }
}
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;
    }
}
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;
    }
}
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;
    }
}
<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>
<?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>
<?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>
<subreport>
    <reportElement x="0" y="30" width="560" height="150"/>
    <subreportExpression><![CDATA["TestSubreport.jasper"]]></subreportExpression>
</subreport>