问题描述
我正在尝试通过将自定义数据源传递给它来填充表。
我创建了一个包含表格的简单报告。报告它自己从ms sql数据库获取数据。我在这个的。但我在桌上没有任何价值。在该示例中没有scriptlet。我检查了(String)this.getFieldValue(KN_FormelGG);
代码行。它从字段中获取数据并可以在报告中显示。所以我猜bean数据源没有填充。我在 afterGroupInit
中调用了填充表方法。如何在jasper中使用java中的数据集合?我还尝试在数据集和查询对话框中添加java bean,但它也没有帮助我。我应该将scriptlet添加到子报表/表吗?我的问题的主要重点是在scriptlet中具有自定义数据源。我通过了,但我仍然没有得到答案。我添加了 $ P {FieldDataSource} .getData()
来检查数据,但它提供了null。
I am trying to fill a table by passing a custom data source to it.I have created a simple report with a table on it. The report it self gets the data from a ms sql database. I have written a java class similar to the class in this Example. But I get no value in table. At the example there is no scriptlet. I have checked the (String) this.getFieldValue("KN_FormelGG");
line of code. It gets the data from field and can show it on report. So I guess the bean data source is not filled. I call the fill Table method in a afterGroupInit
. How can I use Collection of data from java in jasper? I tried also adding the java bean in dataset and query dialog, but it did not help me either. Should I add the scriptlet to subreport/table? The main emphasis of my problem having the custom data source in a scriptlet. I went the other problem through, but I still get no answer. I added $P{FieldDataSource}.getData()
to check the data, but it delivers null.
java class 1:
package testProjektIman.scriptlets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
public class FillTable extends JRDefaultScriptlet {
@Override
public void afterGroupInit(final String id)
throws JRScriptletException {
fillTable();
}
public ArrayList<String> splitGGArray(final String kNFormelGG) {
ArrayList<String> fieldNames = new ArrayList<>();
String[] array = (kNFormelGG.split(" "));
for (String sub : array) {
fieldNames.add(sub);
}
return fieldNames;
}
public Map<String, Object> fillTable()
throws JRScriptletException {
String kNFormelGG = null;
kNFormelGG = (String) this.getFieldValue("KN_FormelGG");
List<TableCells> listTableCells = new ArrayList<>();
TableCells tableCell;
for (String fn : splitGGArray(kNFormelGG)) {
tableCell = new TableCells();
tableCell.setFieldName(fn);
listTableCells.add(tableCell);
}
JRBeanCollectionDataSource tableCellJRBean = new JRBeanCollectionDataSource(listTableCells);
Map<String, Object> parameters = new HashMap<>();
parameters.put("FieldDataSource", tableCellJRBean);
return parameters;
}
}
Java class 2
package testProjektIman.scriptlets;
public class TableCells {
private String fieldName;
private String keyFormel;
private String mK;
private String notation;
private String Item;
//getters setters
}
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="tableAutoFill" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="37fc3a9c-38e9-41be-9039-56249c5283d7">
<subDataset name="TableDataSource" uuid="5999e646-aeec-4b2b-b29b-68e897d56999">
<queryString>
<![CDATA[]]>
</queryString>
<field name="fieldName" class="java.lang.String"/>
</subDataset>
<scriptlet name="Filltable" class="testProjektIman.scriptlets.FillTable"/>
<parameter name="FieldDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource" isForPrompting="false"/>
<queryString>
<![CDATA[select * from "KennzahlReferenz2015_QIBericht", "Images"
where LB_ID = 62
and KN_OffiziellGruppe = 3
and IMG_ID = 1]]>
</queryString>
<field name="KN_Id" class="java.lang.Integer"/>
<field name="KN_FormelZ" class="java.lang.String"/>
<field name="KN_FormelGG" class="java.lang.String"/>
<group name="id">
<groupExpression><![CDATA[$F{KN_Id}]]></groupExpression>
<groupHeader>
<band height="50"/>
</groupHeader>
<groupFooter>
<band height="50"/>
</groupFooter>
</group>
<detail>
<band height="191" splitType="Stretch">
<componentElement>
<reportElement x="50" y="18" width="260" height="120" uuid="942ab836-df83-4a2f-8215-845073ad163f">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
<property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
<property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
<property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
</reportElement>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" whenNoDataType="AllSectionsNoDetail">
<datasetRun subDataset="TableDataSource" uuid="b554ada5-c388-4534-af8e-93571a417adb">
<parametersMapExpression><![CDATA[$P{FieldDataSource}]]></parametersMapExpression>
<dataSourceExpression><![CDATA[$P{FieldDataSource}]]></dataSourceExpression>
</datasetRun>
<jr:column width="100" uuid="f57e4e8e-8a2e-405f-b9ab-7a704e0986fd">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<jr:columnHeader style="Table_CH" height="30">
<staticText>
<reportElement x="0" y="0" width="100" height="30" uuid="9a55dd73-71e3-4559-9a8b-17d98bf17753"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[FieldName]]></text>
</staticText>
</jr:columnHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="100" height="30" uuid="c3e6ccfc-9f91-4d7a-800a-613f5dded928"/>
<textFieldExpression><![CDATA[$F{fieldName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
</jasperReport>
推荐答案
数据适配器
通过用户界面创建数据适配器文件,例如 adapter.xml
。内容可能类似于:
Data Adapter
Create a data adapter file, for example adapter.xml
, through the user interface. The contents might resemble:
<?xml version="1.0" encoding="UTF-8" ?>
<beanDataAdapter class="net.sf.jasperreports.data.bean.BeanDataAdapterImpl">
<name>YourClass</name>
<factoryClass>com.yourcompany.jasper.JRDataSourceFactory</factoryClass>
<methodName>createCollection</methodName>
<useFieldDescription>false</useFieldDescription>
</beanDataAdapter>
数据类
创建一个类有一个 createCollection
方法,根据上面的数据适配器定义:
Data Class
Create a class that has a createCollection
method, as per the data adapter definition above:
package com.compay.jasper;
public class JRDataSourceFactory {
/**
* @return A collection of data for the report.
*/
public static Collection<YourClass> createCollection() {
return Arrays.asList( new YourClass() );
}
}
设置报告属性
确保报告具有以下属性(将报告链接到自定义数据适配器):
Set Report Properties
Ensure that the report has the following property (links the report to the custom data adapter):
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="adapter.xml"/>
设置报告字段
报告字段现在应该能够引用bean属性了:
Set Report Fields
The report fields should now be able to reference the bean properties:
<field name="yourObject.property" class="java.lang.String">
<fieldDescription><![CDATA[yourObject.property]]></fieldDescription>
</field>
fieldDescription
元素包含一个重要信息反映bean属性的值(即,通常调用的Java代码,用于从该bean的实例上的bean中检索值)。
It's important that the fieldDescription
element contain a value that reflects the bean property (i.e., the Java code you would normally call to retrieve the value from the bean on an instance of that bean).
JRDataSourceFactory
类是独立的 - 数据适配器使用它来创建bean实例的集合。 static
方法( createCollection
)在这里完成工作,但实际上不能使用继承。
The JRDataSourceFactory
class stands alone -- it's used by the data adapter to create a collection of bean instances. The static
method (createCollection
) does the work here and does not, indeed cannot, use inheritance.
如果可能,从bean的属性借用报告的字段名称。问题中写的代码使得很难辨别 KN_Id
的值来自何处。
If possible, borrow the field names for the report from the bean's attributes. The code as written in the question makes it difficult to discern where the value for KN_Id
comes from.
以下示例将数据适配器链接到报表中的字段名称。
The following example links the data adapter to the field names in the report.
bean公开了一些属性:
A bean exposes some properties:
package com.company.domain;
public final class Student extends Entity {
private String firstName;
private String lastName;
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
}
数据适配器
数据适配器如下所示:
Data Adapter
The data adapter looks like:
<?xml version="1.0" encoding="UTF-8" ?>
<beanDataAdapter class="net.sf.jasperreports.data.bean.BeanDataAdapterImpl">
<name>Student</name>
<factoryClass>com.company.jasper.JRDataSourceFactory</factoryClass>
<methodName>createCollection</methodName>
<useFieldDescription>false</useFieldDescription>
</beanDataAdapter>
数据类
数据类创建bean的实例:
Data Class
The data class creates instances of the bean:
package com.compay.jasper;
import com.company.domain.Student;
public class JRDataSourceFactory {
public static Collection<Student> createCollection() {
return Arrays.asList( new Student() );
}
}
报告字段
报告字段反映了bean字段:
Report Fields
The report fields reflect the bean fields:
<field name="firstName" class="java.lang.String">
<fieldDescription><![CDATA[firstName]]></fieldDescription>
</field>
<field name="lastName" class="java.lang.String">
<fieldDescription><![CDATA[lastName]]></fieldDescription>
</field>
将学生实例的集合传递到报告中。在此示例中,集合包含单个实例。在您的示例中,集合可以包含许多实例。当报表库迭代集合时, firstName
和 lastName
的不同值将可用。
A collection of "student" instances is passed into the report. In this example, the collection contains a single instance. In your example, the collection could contain many instances. As the report library iterates over the collection, different values of firstName
and lastName
will be made available.
最初填充 Student
实例数据的机制超出了本答案的范围。就报告工具而言,它只使用预先填充的 Student
实例。如果 FillTable
填充 TableCells
,那么这不是报告工具的关注点。
The mechanics for how the Student
instance data is initially populated is outside the scope of this answer. As far as the reporting tool is concerned, it simply uses pre-populated instances of Student
. If FillTable
populates TableCells
, that's not a concern for the reporting tool.
这篇关于将自定义数据源添加到Jaspersoft Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!