问题
I'm having trouble generating a basic report (master/subreport) with JavaBeans in Jaspersoft Studio.
I created TestMainReport.jrxml and TestSubreport.jrxml.
TestMainReport.jrxml contains two static text fields, labeled "A Title"
in the title band and "A Summary"
in the summary band.
TestSubreport.jrxml contains two static text fields, "Subreport Title"
in the title and "Subreport Summary"
in the summary band.
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.
What am I doing wrong?
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
<?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
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;
}
}
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
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
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;
}
}
Output from TestMainReport.jrxml:
Output from TestSubreport.jrxml:
回答1:
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.
Solution1 - Using dataSourceExpression
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>
In this case CustomerAddressDataSource class will be used for filling subreport.
Solution 2 - Using external definition of Data Adapter
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).
You can export Data Adapter definition with help of context menu at JSS (Jaspersoft Studio):
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>
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
.
The subreport template will be:
<?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.
As for the first solution the output result at JSS will be:
More information
Excel Data Adapter Sample
JSONQL Data Source Sample
Excel Data Adapter Sample
Using Data Adapters in Reports and Datasets
Data Adapters
来源:https://stackoverflow.com/questions/56298359/the-master-report-does-not-show-basic-subreport-with-java-beans-at-jaspersoft-st