在能够根据Add custom data source to Jaspersoft Studio将自定义数据源从 java bean 添加到报告后,我使用 jasper 进行报告的第二点。
我有一个使用数据库作为数据源的主报告。然后我在报表中添加一个 bean.xml 数据源,并在主报表中添加一个表,该表使用这个 bean.xml 数据源来获取 Java bean。
我的目标是从主报告中获取一个字段值并操作它的值,然后用这些值填充 bean,最后用 bean 填充表。
为此,我编写了 3 个类,我在表数据集中用作Scriptlet:
问题出在 FillTable 类中,当我String kNFormelGG = (String) this.getParameterValue("gg");
使用创建的 bean.xml 时,测试连接失败java.lang.reflect.InvocationTargetException
Caused by: java.lang.NullPointerException
at net.sf.jasperreports.engine.JRAbstractScriptlet.getParameterValue(JRAbstractScriptlet.java:95)
at net.sf.jasperreports.engine.JRAbstractScriptlet.getParameterValue(JRAbstractScriptlet.java:86)
at org.iqtig.reporting.dataSource.bean.dataSourceXML.FillTable.fillTable(FillTable.java:45)
at org.iqtig.reporting.dataSource.bean.dataSourceXML.JRDataSourceFactory.createCollection(JRDataSourceFactory.java:27)
... 34 more
如果我分配一个固定值String kNFormelGG ="Test me"
,例如 bean 连接没有遇到错误,并且在将 bean.xml 分配为 Dataset1 中的默认数据适配器的值之后,它会用静态值填充表。如何从主报表数据源动态获取参数或值的数据并在 bean 中使用?我假设在从我的适配器调用静态工厂类时,这些字段仍然是空的。也许我错了,但我没有找到任何其他关于这个问题的声明。
BeanFactory 类
import java.util.Collection;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRScriptletException;
/**
* Factory for TableCellsBean Klasse
*
* @author iman.gharib
*/
public class JRDataSourceFactory extends JRDefaultScriptlet {
/**
* @return collection der TableCellsBean Objekten
* @throws JRScriptletException
*/
public static Collection<TableCellsBean> createCollection()
throws JRScriptletException {
FillTable ft = new FillTable();
Collection<TableCellsBean> reportRows = ft.fillTable();
return reportRows;
}
}
豆类
public class TableCellsBean {
private String fieldName;
private String keyFormel;
private String mK;
private String notation;
private String item;
/**
* Constructor.
*
* @param fieldName
* @param keyFormel
* @param mK
* @param notation
* @param item
*/
public TableCellsBean(final String fieldName, final String keyFormel, final String mK, final String notation, final String item) {
this.fieldName = fieldName;
this.keyFormel = keyFormel;
this.mK = mK;
this.notation = notation;
this.item = item;
}
/**
* Constructo Leer
*/
public TableCellsBean() {
}
public TableCellsBean getme() {
return this;
}
// getter and setters
}
准备和创建 bean 的类
public class FillTable extends JRDefaultScriptlet {
@Override
public void afterColumnInit()
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 Collection<TableCellsBean> fillTable()
throws JRScriptletException {
// gg is a parameter for table dataset. It is mapped to KN_FormelGG
// which comes from the main report data base
String kNFormelGG = (String) this.getParameterValue("gg");
List<TableCellsBean> listTableCells = new ArrayList<>();
// TableCellsBean tableCell = new TableCellsBean();
for (String fn : splitGGArray(kNFormelGG)) {
listTableCells.add(new TableCellsBean(fn, fn, fn, fn, fn));
// listTableCells.add(tableCell);
}
// JRBeanCollectionDataSource tableCellJRBean = new JRBeanCollectionDataSource(listTableCells);
// Map<String, Object> parameters = new HashMap<>();
// parameters.put("FieldDataSource", tableCellJRBean);
return listTableCells;
}
}
JRXML
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.0.final using JasperReports Library version 6.3.0 -->
<!-- 2016-08-08T14:30:03 -->
<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="main" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4f1480cf-f8f9-420f-96b4-7fc1e41e791b">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="QIDBReport"/>
<style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<subDataset name="Dataset1" uuid="5677929d-813b-4d39-828c-de966a9d7689">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="bean.xml"/>
<property name="net.sf.jasperreports.data.adapter" value="bean.xml"/>
<scriptlet name="Scriptlet_1" class="org.iqtig.reporting.dataSource.bean.mapBeanAsDatasource.JRDataSourceFactory"/>
<parameter name="gg" class="java.lang.String"/>
<field name="item" class="java.lang.String">
<fieldDescription><![CDATA[item]]></fieldDescription>
</field>
<field name="fieldName" class="java.lang.String">
<fieldDescription><![CDATA[fieldName]]></fieldDescription>
</field>
<field name="me" class="org.iqtig.reporting.dataSource.bean.dataSourceXML.TableCellsBean">
<fieldDescription><![CDATA[me]]></fieldDescription>
</field>
<field name="keyFormel" class="java.lang.String">
<fieldDescription><![CDATA[keyFormel]]></fieldDescription>
</field>
<field name="mK" class="java.lang.String">
<fieldDescription><![CDATA[mK]]></fieldDescription>
</field>
</subDataset>
<parameter name="LB_ID" class="java.lang.Integer">
<defaultValueExpression><![CDATA[62]]></defaultValueExpression>
</parameter>
<parameter name="KN_OffeziellGruppe" class="java.lang.Integer">
<defaultValueExpression><![CDATA[3]]></defaultValueExpression>
</parameter>
<parameter name="FieldDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource" isForPrompting="false"/>
<parameter name="KN_FormelGG" class="java.lang.String" isForPrompting="false"/>
<queryString>
<![CDATA[select * from "KennzahlReferenz2015_QIBericht", "Images"
where LB_ID = $P{LB_ID}
and KN_OffiziellGruppe = $P{KN_OffeziellGruppe}
and IMG_ID = 1]]>
</queryString>
<field name="QI_Praefix" class="java.lang.String"/>
<field name="KN_Id" class="java.lang.Integer"/>
<field name="bewertungsArtTypNameKurz" class="java.lang.String"/>
<field name="refbereich" class="java.lang.String"/>
<field name="refbereichVorjahres" class="java.lang.String"/>
<field name="KN_GGAlleinstehend" class="java.lang.String"/>
<field name="erlaueterungDerRechregeln" class="java.lang.String"/>
<field name="teildatensatzbezug" class="java.lang.String"/>
<field name="mindesanzahlZaeler" class="java.lang.Integer"/>
<field name="mindesanzahlNenner" class="java.lang.Integer"/>
<field name="KN_FormelZ" class="java.lang.String"/>
<field name="KN_FormelGG" class="java.lang.String"/>
<field name="verwendeteFunktionen" class="java.lang.String"/>
<field name="idLb" class="java.lang.String"/>
<field name="LB_LangBezeichnung" class="java.lang.String"/>
<field name="LB_ID" class="java.lang.Integer"/>
<field name="nameAlleinstehend" class="java.lang.String"/>
<field name="KN_BezeichnungAlleinstehendKurz" class="java.lang.String"/>
<field name="QI_ID" class="java.lang.Integer"/>
<field name="IMG_ID" class="java.lang.Integer"/>
<field name="Name" class="java.lang.String"/>
<field name="Image" class="java.lang.Object"/>
<group name="id" isStartNewPage="true">
<groupExpression><![CDATA[$F{KN_Id}]]></groupExpression>
<groupHeader>
<band height="44"/>
</groupHeader>
<groupFooter>
<band height="50"/>
</groupFooter>
</group>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="44" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="34" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="149" splitType="Stretch">
<componentElement>
<reportElement x="170" y="20" width="350" height="100" uuid="38d917fb-dfc2-4c08-890a-09cfe6e2214d">
<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"/>
<property name="net.sf.jasperreports.export.headertoolbar.table.name" value=""/>
<property name="com.jaspersoft.studio.components.autoresize.proportional" value="true"/>
</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="Dataset1" uuid="1b3548f6-7d6b-4070-bb8e-aaefbabdc7c9">
<datasetParameter name="gg">
<datasetParameterExpression><![CDATA[$F{KN_FormelGG}]]></datasetParameterExpression>
</datasetParameter>
</datasetRun>
<jr:column width="70" uuid="048812d7-0ed1-4db8-a09a-e6242f77c6ce">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
<jr:tableHeader style="Table_TH" height="30" rowSpan="1"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="70" height="30" uuid="c5aaea84-1367-41df-be8d-7f71e3ea5153"/>
<textFieldExpression><![CDATA[$F{item}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="70" uuid="11b85ada-c9fe-42b6-a646-8bd1697cdec2">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
<jr:tableHeader style="Table_TH" height="30" rowSpan="1"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="70" height="30" uuid="728ff44d-1dbd-404c-b8b3-7cc0e1f07f60"/>
<textFieldExpression><![CDATA[$F{fieldName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="70" uuid="892f30cb-fb41-444f-889b-1e005484c35e">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column3"/>
<jr:tableHeader style="Table_TH" height="30" rowSpan="1"/>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="70" height="30" uuid="e38ac951-71bc-45a6-8ed2-313805a77050"/>
<textFieldExpression><![CDATA[$F{keyFormel}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="70" uuid="7d0d700a-5a75-4c26-94c0-9ef7c53bd719">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column4"/>
<jr:tableHeader style="Table_TH" height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="70" height="30" uuid="68577007-0344-406c-8aa2-3127d1da1c65"/>
</textField>
</jr:tableHeader>
<jr:detailCell style="Table_TD" height="30">
<textField>
<reportElement x="0" y="0" width="70" height="30" uuid="873d63c1-1b91-4441-b7bd-f67db7729e7f"/>
<textFieldExpression><![CDATA[$F{mK}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="70" uuid="cf5a1a2f-594d-429f-8925-62d001e1dd00">
<property name="com.jaspersoft.studio.components.table.model.column.name" value="Column5"/>
<jr:tableHeader style="Table_TH" height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="70" height="30" uuid="7fb46eb8-d0e1-44ab-89f9-ec31d49b8109"/>
<textFieldExpression><![CDATA[$P{gg}]]></textFieldExpression>
</textField>
</jr:tableHeader>
<jr:detailCell style="Table_TD" height="30"/>
</jr:column>
</jr:table>
</componentElement>
<textField>
<reportElement x="20" y="80" width="100" height="30" uuid="b89cd04c-2569-43ef-9730-445b874855dd"/>
<textFieldExpression><![CDATA[$F{KN_FormelGG}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="20" y="32" width="100" height="30" uuid="e91b4461-5e53-4f85-8992-14e69a1aa05f"/>
<text><![CDATA[KN_FormelGG]]></text>
</staticText>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>