首页 > 解决方案 > Mapfish Print 3 JSON 数据源生成空表字段

问题描述

上周我一直在努力解决这个问题,但我无法解决。在带有浏览器的 Tomcat 上使用 Mapfish Print 3 我正在尝试打印在 Jaspersoft Studio 中生成的 .jrxml 模板。模板的数据源是一个 .json,包含表格。

当我从 jaspersoft studio 打印 pdf 时,一切正常,但是当我从 Mapfish 打印它时,我的表的字段没有被填充,所以我有正确的表列名称,并且下面只有一行填充了“null”。

Tomcat 日志抛出此错误:

gradle: 09:34:01.835 [PrintJobManager-3] WARN  o.m.p.o.AbstractJasperReportOutputFormat - The field letter in prova_json.jrxml is not available in at least one of the rows in the datasource.  This may not be an error.
gradle: 09:34:01.835 [PrintJobManager-3] WARN  o.m.p.o.AbstractJasperReportOutputFormat - The field number in prova_json.jrxml is not available in at least one of the rows in the datasource.  This may not be an error.

实际结果 期望结果

我查看了模板数据适配器、.yaml 和 .json 配置,但一无所获。我对此很陌生,我可能会忘记一些基本的东西。

如果有人可以提供帮助,我会非常高兴。

非常感谢

这是我的 .yaml:

templates:
  A4 portrait: !template
    reportTemplate: prova_json.jrxml
    attributes:
      title: !string
        default: "Mytable"
    processors:
    - !reportBuilder
      directory: '.'

这是我的 .json:

{
  "layout": "A4 portrait",
  "outputFormat": "pdf",
  "attributes": {},
  "example":[
    {"letter":"A", "number":"1"},
    {"letter":"B", "number":"2"},
    {"letter":"C", "number":"3"}
              ]
}

这是我的 .jrxml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
<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="prova_json" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="443dccec-8ac9-486f-b9e2-bf8809609b24">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="requestData"/>
    <parameter name="title" class="java.lang.String"/>
    <queryString language="json">
        <![CDATA[example]]>
    </queryString>
    <field name="letter" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="letter"/>
        <fieldDescription><![CDATA[letter]]></fieldDescription>
    </field>
    <field name="number" class="java.lang.Integer">
        <property name="net.sf.jasperreports.json.field.expression" value="number"/>
        <fieldDescription><![CDATA[number]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="50" splitType="Stretch">
            <textField>
                <reportElement x="230" y="20" width="100" height="30" uuid="e15aa58f-c3f8-415c-b783-9d99cd9fd936"/>
                <box>
                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="30" splitType="Stretch">
            <staticText>
                <reportElement x="100" y="0" width="180" height="30" uuid="710e904e-be4b-4827-bed5-cba2caa930af">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="fca73b4c-ec21-4d8e-8103-e847788aa7d0"/>
                </reportElement>
                <box>
                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[letter]]></text>
            </staticText>
            <staticText>
                <reportElement x="280" y="0" width="175" height="30" uuid="2434f475-1bf1-428b-9eda-428133d8f1e3">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="70d93515-4ef5-4746-acac-b96c8b4f43c9"/>
                </reportElement>
                <box>
                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[number]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="100" y="0" width="180" height="30" uuid="bf808ed8-1e3e-4d6f-9e62-03c2491b89cd">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="fca73b4c-ec21-4d8e-8103-e847788aa7d0"/>
                </reportElement>
                <box>
                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{letter}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="280" y="0" width="175" height="30" uuid="e53139cc-0aee-4f5a-8b45-55123ae024fd">
                    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="70d93515-4ef5-4746-acac-b96c8b4f43c9"/>
                </reportElement>
                <box>
                    <topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{number}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

标签: jsondatasourcemapfish

解决方案


在需要设置属性示例的属性中(或任何数据集的名称),在需要添加 !createDataSource 处理器的处理器中,最后 yaml 应如下所示:

templates: reportTemplate: prova_json.jrxml attributes: title: !string default:"title" example: !datasource attributes: title: !string table: !table{} processors: - !reportBuilder directory: '.' - !createDataSource processors: - !prepareTable dynamic: true columns: Icon: !urlImage urlExtractor: (.*) urlGroup: 1 tableData: jrDataSource

至少在 mapfishprint 的演示中是这样的,你也可以在github上找到它们


推荐阅读