Java 如何将任意对象作为参数传递给 jasper 报告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2684025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I pass an arbitrary object to jasper report as parameter?
提问by
I would like to pass as a parameter to my .jrxml an arbitrary object of my domain, e.g a Person.
我想将我的域的任意对象作为参数传递给我的 .jrxml,例如一个人。
InputStream reportFile = MyPage.this.getClass().getResourceAsStream("test.jrxml");
HashMap<String, Person> parameters = new HashMap<String, Person>();
parameters.put("person", new Person("John", "Doe"));
...
JasperReport report = JasperCompileManager.compileReport(reportFile);
JasperPrint print = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());
return JasperExportManager.exportReportToPdf(print);
And on the .jrxml do something like:
在 .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="test" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="PERSON" isForPrompting="false" class="myApp.domain.person"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="20">
<staticText>
<reportElement x="180" y="0" width="200" height="20"/>
<text><![CDATA[$P{PERSON.lastName}]]></text>
</staticText>
</band>
</title>
...
Is something like this possible? Where can I find more complex tutorials that show more than just passing a java.lang.String?
这样的事情可能吗?我在哪里可以找到更复杂的教程,这些教程不仅仅展示了传递 java.lang.String 的内容?
Thanks
谢谢
采纳答案by medopal
Yes, you can pass any Java object, but you should make sure to import that object in the JRXML.
是的,您可以传递任何 Java 对象,但您应该确保在 JRXML 中导入该对象。
Inside the jasperReport tag. You can use the tag import
, like:
在 jasperReport 标签内。您可以使用标签import
,例如:
<jasperReport...>
<import value="org.justfortest.Person">
However, you can use JRBeanCollectionDataSource
and populate the report with a list of your object, without needing to store arbitrary objects in the params map.
但是,您可以使用JRBeanCollectionDataSource
对象列表来使用和填充报告,而无需在 params 映射中存储任意对象。
Check this tutorial for more info on Jasper Reports Bean Collection Data Source
查看本教程以获取有关Jasper Reports Bean 集合数据源的更多信息
回答by Bozho
Yes, it is possible exactly the way you explained. Just make sure you have the correct classpath when compiling the jrxml and be careful with the case - either lowercase (person
) or uppercase (PERSON
) in all places.
是的,完全可以按照您解释的方式进行。只要确保在编译 jrxml 时具有正确的类路径,并注意大小写 -在所有地方都可以使用小写 ( person
) 或大写 ( PERSON
)。