Jump to content
Changes to the Jaspersoft community edition download ×

Map/Collection Data Source


finnhawk

Recommended Posts

I'm getting REALLY frustrated here... I need to be able to use a Map or Collection as a Data Source. But when I compile the following jrxml file and run the following java code, I get a blank report. If I change the Data Source to use new JREmptyDataSource() then I get the columns with one row of null values, so I guess there something wrong with my Map but I don't understand what!? What's going on?

 

Report.jrxml

Code:
<?xml version="1.0" encoding="UTF-8"  ?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="Report">
<field name="one" class="java.lang.String" />
<field name="two" class="java.lang.String" />
<pageHeader>
<band height="30">
<staticText>
<reportElement x="0" y="0" width="69" height="24" />
<text><![CDATA[ColumnOne: ]]></text>
</staticText>
<staticText>
<reportElement x="140" y="0" width="79" height="24" />
<text><![CDATA[ColumnTwo: ]]></text>
</staticText>
</band>
</pageHeader>
<detail>
<band height="30">
<textField>
<reportElement x="0" y="0" width="69" height="24" />
<textFieldExpression class="java.lang.String"><![CDATA[$F{one}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="140" y="0" width="69" height="24" />
<textFieldExpression class="java.lang.String"><![CDATA[$F{two}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>

 

Report.java

Code:
[code]import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.*;

public class Report {

public Report() {
HashMap[] row = new HashMap[2];
row[0] = new HashMap();
row[0].put("one", "Mike"«»);
row[0].put("two", "Smith"«»);
row[1] = new HashMap();
row[1].put("one", "John"«»);
row[1].put("two", "Jones"«»);

JRMapArrayDataSource dataSource = new JRMapArrayDataSource(row);

try {
JasperFillManager.fillReportToFile("Report.jasper", new HashMap(), dataSource);
JasperRunManager.runReportToPdfFile("Report.jasper", new HashMap(), dataSource);
JasperRunManager.runReportToHtmlFile("Report.jasper", new HashMap(), dataSource);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Well, I got it to work by taking out the line:

 

JasperFillManager.fillReportToFile("Report.jasper", new HashMap(), dataSource);

 

What is it even supposed to do and why would it interfere with PDF file creation?

Post edited by: finnhawk, at: 2007/04/19 22:34

Link to comment
Share on other sites

Hi,

 

Looking at your original three lines of code:

 

JasperFillManager.fillReportToFile("Report.jasper", new HashMap(), dataSource);

JasperRunManager.runReportToPdfFile("Report.jasper", new HashMap(), dataSource);

JasperRunManager.runReportToHtmlFile("Report.jasper", new HashMap(), dataSource);

 

Not only that the first line is useless, since it produces a file that you don't later use, but it also exhausts your data source by iterating through it and moving the report pointer to the last record. This is why subsequent usages of the data source find it empty, hence nothing gets generated.

 

The fill method you removed, fills the report and produces a JR document in proprietary format with the *.jrprint extension. This file can later be exporter to other formats, but you don't use that file and call the run methods.

These are utility method for those who want to fill and then export on the fly. But in your case, it would be better to first fill and the export the intermediate document to two formats, instead of filling it twice and exporting it twice.

 

So, I suggest you put aside the JasperRunManager and go with the JasperExportManager instead.

 

Reading whatever scarce freely available documentation we have would also help pass such problems.

 

I hope this helps.

Teodor

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...