Clojure:将记录列表传递给 Java 对象
Clojure: passing a list of records to a Java object
我正在试验 Clojure。我能够在 JasperReports 中生成报告,但是
它给出空值。有问题的 Java 对象是 JRBeanCollectionDataSource
。
我向它传递了一个记录列表,但不知何故,PDF 只包含空值。
此外,为什么我无法将它 {}
作为空 HashMap
传递?在 Groovy 中,[:]
语法正常。
(ns jasper.core
(:import
(net.sf.jasperreports.engine JasperCompileManager
JasperFillManager
JasperExportManager)
(net.sf.jasperreports.engine.data JRBeanCollectionDataSource)))
(import 'java.util.HashMap)
(defrecord Car [id name price])
(def data [(->Car 1, "Audi", 52642)
(->Car 2, "Mercedes", 57127)
(->Car 3, "Skoda", 9000)
(->Car 4, "Volvo", 29000)
(->Car 5, "Bentley", 350000)
(->Car 6, "Citroen", 21000)
(->Car 7, "Hummer", 41400)
(->Car 8, "Volkswagen", 21600)])
(def xmlFile "resources/report.xml")
(def jrReport (JasperCompileManager/compileReport xmlFile))
;; (def params {})
(def params (HashMap.))
(def ds (JRBeanCollectionDataSource. data))
(println (.toString ds))
(def jrPrint (JasperFillManager/fillReport jrReport params ds))
(defn -main
[]
(JasperExportManager/exportReportToPdfFile jrPrint "report.pdf"))
这是对以下工作 Groovy 解决方案的重写:
@Grab(group='net.sf.jasperreports', module='jasperreports', version='6.17.0')
import net.sf.jasperreports.engine.JasperCompileManager
import net.sf.jasperreports.engine.JasperFillManager
import net.sf.jasperreports.engine.JasperExportManager
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
import groovy.transform.Immutable
@Immutable
class Car {
Long id;
String name;
int price;
}
def data = [
new Car(1L, 'Audi', 52642),
new Car(2L, 'Mercedes', 57127),
new Car(3L, 'Skoda', 9000),
new Car(4L, 'Volvo', 29000),
new Car(5L, 'Bentley', 350000),
new Car(6L, 'Citroen', 21000),
new Car(7L, 'Hummer', 41400),
new Car(8L, 'Volkswagen', 21600),
]
def empty = []
def xmlFile = "report.xml"
def jrReport = JasperCompileManager.compileReport(xmlFile)
def ds = new JRBeanCollectionDataSource(data)
def params = [:]
def jrPrint = JasperFillManager.fillReport(jrReport, params, ds)
JasperExportManager.exportReportToPdfFile(jrPrint, "report.pdf")
编辑 对于完整的工作示例,将以下文件放在 Groovy 的当前工作目录中,以及 Clojure 的资源目录中。
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<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"
whenNoDataType="NoDataSection"
name="report" topMargin="20" bottomMargin="20">
<field name="id" class="java.lang.Long"/>
<field name="name"/>
<field name="price" class="java.lang.Integer"/>
<detail>
<band height="15">
<textField>
<reportElement x="0" y="0" width="50" height="15"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression class="java.lang.Long">
<![CDATA[$F{id}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="150" y="0" width="100" height="15" />
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression class="java.lang.String">
<![CDATA[$F{name}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="0" width="100" height="15" />
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression class="java.lang.Integer">
<![CDATA[$F{price}]]>
</textFieldExpression>
</textField>
</band>
</detail>
<noData>
<band height="15">
<staticText>
<reportElement x="0" y="0" width="200" height="15"/>
<box>
<bottomPen lineWidth="1.0" lineColor="#CCCCCC"/>
</box>
<textElement />
<text><![CDATA[The report has no data]]></text>
</staticText>
</band>
</noData>
</jasperReport>
根据评论的提示,我能够解决问题。问题是 Clojure 记录没有实现 JavaBeans 规范,而 JRBeanCollectionDataSource
需要这样的 beans。
使用 clj-bean
库,我能够让它工作。
(defbean Car
[[Long id]
[String name]
[Integer price]])
(def data [(Car. 1 "Audi" 52642),
(Car. 2 "Mercedes" 57127),
(Car. 3 "Skoda" 9000),
(Car. 4 "Volvo" 29000),
(Car. 5 "Bentley" 350000),
(Car. 6 "Citroen" 21000),
(Car. 7 "Hummer" 41400),
(Car. 8 "Volkswagen" 21600)])
现在报告包含数据。
我正在试验 Clojure。我能够在 JasperReports 中生成报告,但是
它给出空值。有问题的 Java 对象是 JRBeanCollectionDataSource
。
我向它传递了一个记录列表,但不知何故,PDF 只包含空值。
此外,为什么我无法将它 {}
作为空 HashMap
传递?在 Groovy 中,[:]
语法正常。
(ns jasper.core
(:import
(net.sf.jasperreports.engine JasperCompileManager
JasperFillManager
JasperExportManager)
(net.sf.jasperreports.engine.data JRBeanCollectionDataSource)))
(import 'java.util.HashMap)
(defrecord Car [id name price])
(def data [(->Car 1, "Audi", 52642)
(->Car 2, "Mercedes", 57127)
(->Car 3, "Skoda", 9000)
(->Car 4, "Volvo", 29000)
(->Car 5, "Bentley", 350000)
(->Car 6, "Citroen", 21000)
(->Car 7, "Hummer", 41400)
(->Car 8, "Volkswagen", 21600)])
(def xmlFile "resources/report.xml")
(def jrReport (JasperCompileManager/compileReport xmlFile))
;; (def params {})
(def params (HashMap.))
(def ds (JRBeanCollectionDataSource. data))
(println (.toString ds))
(def jrPrint (JasperFillManager/fillReport jrReport params ds))
(defn -main
[]
(JasperExportManager/exportReportToPdfFile jrPrint "report.pdf"))
这是对以下工作 Groovy 解决方案的重写:
@Grab(group='net.sf.jasperreports', module='jasperreports', version='6.17.0')
import net.sf.jasperreports.engine.JasperCompileManager
import net.sf.jasperreports.engine.JasperFillManager
import net.sf.jasperreports.engine.JasperExportManager
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
import groovy.transform.Immutable
@Immutable
class Car {
Long id;
String name;
int price;
}
def data = [
new Car(1L, 'Audi', 52642),
new Car(2L, 'Mercedes', 57127),
new Car(3L, 'Skoda', 9000),
new Car(4L, 'Volvo', 29000),
new Car(5L, 'Bentley', 350000),
new Car(6L, 'Citroen', 21000),
new Car(7L, 'Hummer', 41400),
new Car(8L, 'Volkswagen', 21600),
]
def empty = []
def xmlFile = "report.xml"
def jrReport = JasperCompileManager.compileReport(xmlFile)
def ds = new JRBeanCollectionDataSource(data)
def params = [:]
def jrPrint = JasperFillManager.fillReport(jrReport, params, ds)
JasperExportManager.exportReportToPdfFile(jrPrint, "report.pdf")
编辑 对于完整的工作示例,将以下文件放在 Groovy 的当前工作目录中,以及 Clojure 的资源目录中。
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<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"
whenNoDataType="NoDataSection"
name="report" topMargin="20" bottomMargin="20">
<field name="id" class="java.lang.Long"/>
<field name="name"/>
<field name="price" class="java.lang.Integer"/>
<detail>
<band height="15">
<textField>
<reportElement x="0" y="0" width="50" height="15"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression class="java.lang.Long">
<![CDATA[$F{id}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="150" y="0" width="100" height="15" />
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression class="java.lang.String">
<![CDATA[$F{name}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="0" width="100" height="15" />
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression class="java.lang.Integer">
<![CDATA[$F{price}]]>
</textFieldExpression>
</textField>
</band>
</detail>
<noData>
<band height="15">
<staticText>
<reportElement x="0" y="0" width="200" height="15"/>
<box>
<bottomPen lineWidth="1.0" lineColor="#CCCCCC"/>
</box>
<textElement />
<text><![CDATA[The report has no data]]></text>
</staticText>
</band>
</noData>
</jasperReport>
根据评论的提示,我能够解决问题。问题是 Clojure 记录没有实现 JavaBeans 规范,而 JRBeanCollectionDataSource
需要这样的 beans。
使用 clj-bean
库,我能够让它工作。
(defbean Car
[[Long id]
[String name]
[Integer price]])
(def data [(Car. 1 "Audi" 52642),
(Car. 2 "Mercedes" 57127),
(Car. 3 "Skoda" 9000),
(Car. 4 "Volvo" 29000),
(Car. 5 "Bentley" 350000),
(Car. 6 "Citroen" 21000),
(Car. 7 "Hummer" 41400),
(Car. 8 "Volkswagen" 21600)])
现在报告包含数据。