11

I would like to pass as a parameter to my .jrxml an arbitrary object of my domain, e.g a Person.

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:

<?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?

Thanks

2 Answers 2

13

Yes, you can pass any Java object, but you should make sure to import that object in the JRXML.

Inside the jasperReport tag. You can use the tag import, like:

 <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.

Check this tutorial for more info on Jasper Reports Bean Collection Data Source

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

Thanks, what if I don't want to set a particular classpath, can I put in the .jrxml something like <import package=".."> or something like that for the report to be able to find a particular class? Thanks again for your help!
I think you can use import. Be sure to report whether your attempt was successful.
Very relevant question, were you able to solve it? Facing exactly the same requirements, as we need to upload jrxml to jasper server and we obviously dont want to add anything to class path on the server(not easy to update), at the same time we dont want to use <resources> of report unit but rather import classes into jrxml directly. Any suggestions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.