Jump to content
Changes to the Jaspersoft community edition download ×

Pass JRDataSource to SubReport


2004 IR Help

Recommended Posts

By: new2jasper - villihelga

Pass JRDataSource to SubReport

2005-05-24 05:29

How do I do this?

 

I have the the data source set to "Use Connection Expression" : $P{REPORT_CONNECTION}

but I am guessing it should be "Use Data Source Expression", but dont know what to put instead of $P{MyDataource}

 

My datasource is a JRMapCollection:

JasperPrint jasperPrint =

JasperFillManager.fillReport(

jasperReport,

parameters,

new JRMapCollectionDataSource(Arrays.asList(dataMapArray()))

));

 

 

 

 

 

 

 

By: new2jasper - villihelga

RE: Pass JRDataSource to SubReport

2005-05-25 02:54

I tried that, but get a java.lang.ClassCastException: java.util.HashMap exception.

I defined my parameter in the main report, and the sub reports as java.util.HashMap, but still get the error? Any ideas?

 

 

 

 

By: Mitya - mityay

RE: Pass JRDataSource to SubReport

2005-05-25 03:52

Are you sure, that you have Maps in you collection?

Write here your jrxml, server code and stack trace for error.

 

 

 

 

By: new2jasper - villihelga

RE: Pass JRDataSource to SubReport

2005-05-25 04:04

Thanks for your help. I am a complete newby when it comes to JasperReports, and your help is greatly appreciated.

 

 

I am using iReport 0.3.2 and JasperReports 0.6.6

 

 

JasperDesign jasperDesign = JRXmlLoader.load("ODSP001.jrxml");

JasperReport jasperReport =

JasperCompileManager.compileReport(jasperDesign);

 

JasperDesign jasperDesignSub = JRXmlLoader.load("ODSPAcceptance.jrxml");

JasperCompileManager.compileReportToFile(jasperDesignSub, "ODSPAcceptance.jasper");

 

 

 

 

// Map SubReport

Map paraSub = new HashMap();

paraSub.put("AcceptanceName","The sub map exceptance name");

paraSub.put("Test","The sub map test value");

 

// Map of parameters passed to the report.

Map parameters = new HashMap();

parameters.put("Title", "ODSP Test Report");

parameters.put("ProgramName", "The Program Name");

parameters.put("dataSourceSub", paraSub);

 

JasperPrint jasperPrint =

JasperFillManager.fillReport(

jasperReport,

parameters,

new JRMapCollectionDataSource(Arrays.asList(dataMapArray()))

);

 

 

 

jrxml for the main form:

 

<?xml version="1.0" encoding="UTF-8" ?>

<!-- Created with iReport - A designer for JasperReports -->

<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport

name="ODSPAcceptance"

columnCount="1"

printOrder="Vertical"

orientation="Landscape"

pageWidth="842"

pageHeight="595"

columnWidth="535"

columnSpacing="0"

leftMargin="30"

rightMargin="30"

topMargin="20"

bottomMargin="20"

whenNoDataType="NoPages"

isTitleNewPage="false"

isSummaryNewPage="false">

<property name="ireport.scriptlethandling" value="2" />

<parameter name="Title" isForPrompting="false" class="java.lang.String"/>

<parameter name="SubReport" isForPrompting="true" class="java.lang.String"/>

<parameter name="dataSourceSub" isForPrompting="false" class="java.util.Map"/>

<field name="AcceptanceName" class="java.lang.String"/>

<background>

<band height="0" isSplitAllowed="true" >

</band>

</background>

<title>

<band height="50" isSplitAllowed="true" >

</band>

</title>

<pageHeader>

<band height="50" isSplitAllowed="true" >

<staticText>

<reportElement

mode="Opaque"

x="495"

y="10"

width="239"

height="28"

forecolor="#000000"

backcolor="#FFFFFF"

key="staticText-2"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="24" isBold="true" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<text><![CDATA[Acceptance Report]]></text>

</staticText>

</band>

</pageHeader>

<columnHeader>

<band height="30" isSplitAllowed="true" >

<textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" > <reportElement

mode="Opaque"

x="497"

y="9"

width="144"

height="16"

forecolor="#000000"

backcolor="#FFFFFF"

key="textField-2"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[$P{SubReport}]]></textFieldExpression>

</textField>

</band>

</columnHeader>

<detail>

<band height="100" isSplitAllowed="true" >

<staticText>

<reportElement

mode="Opaque"

x="6"

y="25"

width="175"

height="18"

forecolor="#000000"

backcolor="#FFFFFF"

key="staticText-1"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<text><![CDATA[Acceptance Name]]></text>

</staticText>

<textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" > <reportElement

mode="Opaque"

x="191"

y="25"

width="224"

height="20"

forecolor="#000000"

backcolor="#FFFFFF"

key="textField-1"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[$P{Title}]]></textFieldExpression>

</textField>

</band>

</detail>

<columnFooter>

<band height="30" isSplitAllowed="true" >

</band>

</columnFooter>

<pageFooter>

<band height="50" isSplitAllowed="true" >

</band>

</pageFooter>

<summary>

<band height="50" isSplitAllowed="true" >

</band>

</summary>

</jasperReport>

 

 

jrxml for the sub form:

 

<?xml version="1.0" encoding="UTF-8" ?>

<!-- Created with iReport - A designer for JasperReports -->

<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport

name="ODSPAcceptance"

columnCount="1"

printOrder="Vertical"

orientation="Landscape"

pageWidth="842"

pageHeight="595"

columnWidth="535"

columnSpacing="0"

leftMargin="30"

rightMargin="30"

topMargin="20"

bottomMargin="20"

whenNoDataType="NoPages"

isTitleNewPage="false"

isSummaryNewPage="false">

<property name="ireport.scriptlethandling" value="2" />

<parameter name="Title" isForPrompting="false" class="java.lang.String"/>

<parameter name="SubReport" isForPrompting="true" class="java.lang.String"/>

<parameter name="dataSourceSub" isForPrompting="false" class="net.sf.jasperreports.engine.data.JRMapCollectionDataSource"/>

<field name="AcceptanceName" class="java.lang.String"/>

<background>

<band height="0" isSplitAllowed="true" >

</band>

</background>

<title>

<band height="50" isSplitAllowed="true" >

</band>

</title>

<pageHeader>

<band height="50" isSplitAllowed="true" >

<staticText>

<reportElement

mode="Opaque"

x="495"

y="10"

width="239"

height="28"

forecolor="#000000"

backcolor="#FFFFFF"

key="staticText-2"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="24" isBold="true" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<text><![CDATA[Acceptance Report]]></text>

</staticText>

</band>

</pageHeader>

<columnHeader>

<band height="30" isSplitAllowed="true" >

<textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" > <reportElement

mode="Opaque"

x="497"

y="9"

width="144"

height="16"

forecolor="#000000"

backcolor="#FFFFFF"

key="textField-2"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[$P{SubReport}]]></textFieldExpression>

</textField>

</band>

</columnHeader>

<detail>

<band height="100" isSplitAllowed="true" >

<staticText>

<reportElement

mode="Opaque"

x="6"

y="25"

width="175"

height="18"

forecolor="#000000"

backcolor="#FFFFFF"

key="staticText-1"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<text><![CDATA[Acceptance Name]]></text>

</staticText>

<textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" > <reportElement

mode="Opaque"

x="191"

y="25"

width="224"

height="20"

forecolor="#000000"

backcolor="#FFFFFF"

key="textField-1"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[$P{Title}]]></textFieldExpression>

</textField>

</band>

</detail>

<columnFooter>

<band height="30" isSplitAllowed="true" >

</band>

</columnFooter>

<pageFooter>

<band height="50" isSplitAllowed="true" >

</band>

</pageFooter>

<summary>

<band height="50" isSplitAllowed="true" >

</band>

</summary>

</jasperReport>

 

 

 

 

 

By: Andreas Icecold - cubcuz

RE: Pass JRDataSource to SubReport

2005-05-25 04:06

if you want to use the Parameter as datasource for your subreport, it has to implement JRDataSource ( like JRMapCollection or an own one) and should be defined in the masterreport like that:

 

<parameter name="yourSubreportDataSourceParameter" class="net.sf.jasperreports.engine.data.JRMapCollectionDataSource"/>

 

then you can pass it to your subreport as following:

 

<subreport>

<reportElement .. />

<datasourceExpression>

$P{yourSubreportDataSourceParameter}

</datasourceExpression>

<suportExpression ... > your Subreport </subreportExpression>

</subreport>

 

 

 

 

 

By: new2jasper - villihelga

RE: Pass JRDataSource to SubReport

2005-05-25 04:07

And stack trace:

java.lang.ClassCastException: java.util.HashMap

at ODSP001.evaluate(ODSP001:147)

at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:465)

at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:393)

at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:328)

at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:340)

at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:1255)

at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:587)

at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:211)

at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:119)

at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:613)

at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:117)

at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:429)

at com.swissre.odp.report.Main.main(Main.java:57)

 

NESTED BY :

net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression : dataSource_1

Source text : $P{dataSourceSub}

at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:476)

at net.sf.jasperreports.engine.fill.JRCalculator.evaluate(JRCalculator.java:393)

at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:328)

at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:340)

at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:1255)

at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:587)

at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:211)

at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:119)

at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:613)

at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:117)

at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:429)

at com.swissre.odp.report.Main.main(Main.java:57)

 

 

 

 

 

By: Mitya - mityay

RE: Pass JRDataSource to SubReport

2005-05-25 04:19

In your sub form you have to use not

<parameter name="dataSourceSub" isForPrompting="false" class="net.sf.jasperreports.engine.data.JRMapCollectionDataSource"/>

but

<parameter name="dataSourceSub" isForPrompting="false" class="java.util.Map"/>

This is a reason for your classcastexception.

But i don't find any subreport tags in your code.

 

 

 

 

By: Andreas Icecold - cubcuz

RE: Pass JRDataSource to SubReport

2005-05-25 04:24

your code can?t work

you have to nest a subreport in your mainForm like i wrote before in one of your sections. You have to write your compiled subreport into a file so your mainReport

can load the Object from it.

 

<detail><band height="20">

...

<subreport>...

<subreportExpression>Path to your Report</subreportExpression>

</subreport>

...

</detail>

 

 

in your subreport you have only to define a field to

get the single values of the passed datasource

 

<field name="record" class="java.lang.Object" />

 

then you can work with it in your subreport like

 

<textField>...

<textFieldExpression>$F{record}</textFieldExpression>

</textField>

 

Best you look in the subreport sample

 

cub

 

 

 

 

 

 

 

 

 

 

 

 

By: new2jasper - villihelga

RE: Pass JRDataSource to SubReport

2005-05-25 04:25

hummm... must have screwed up when copy pasting.

 

in main report:

 

<parameter name="dataSourceSub" isForPrompting="false" class="java.util.Map"/>

 

..and..

<band height="100" isSplitAllowed="true" >

<subreport isUsingCache="false">

<reportElement

mode="Opaque"

x="21"

y="20"

width="746"

height="22"

forecolor="#000000"

backcolor="#FFFFFF"

key="subreport-1"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<subreportParameter name="SubReport">

<subreportParameterExpression><![CDATA[$F{SubReport}]]></subreportParameterExpression>

</subreportParameter>

<subreportParameter name="dataSourceSub">

<subreportParameterExpression><![CDATA[$P{dataSourceSub}]]></subreportParameterExpression>

</subreportParameter>

<subreportParameter name="paraMaster">

<subreportParameterExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}]]></subreportParameterExpression>

</subreportParameter>

<subreportExpression class="java.lang.String"><![CDATA["ODSPAcceptance.jasper"]]></subreportExpression>

</subreport>

</band>

 

 

 

in sub report I have:

<parameter name="dataSourceSub" isForPrompting="false" class="java.util.Map"/>

 

 

 

 

By: new2jasper - villihelga

RE: Pass JRDataSource to SubReport

2005-05-25 04:28

And it works now. I set the properties on the sub report to not use data source or expression.

But the sub report is displayed as a blank page.

 

 

 

 

 

By: Andreas Icecold - cubcuz

RE: Pass JRDataSource to SubReport

2005-05-25 04:35

you want to display the values of a datasouce of type JRMapCollectionDataSource in your subreport, am i right?

 

 

 

 

 

By: Mitya - mityay

RE: Pass JRDataSource to SubReport

2005-05-25 04:36

Icecold wrote you, but repeate once again:

look in the subreport sample.

It is very simple and all became clear for you.

Define parametersMapExpression, dataSourceExpression

 

 

 

 

By: new2jasper - villihelga

RE: Pass JRDataSource to SubReport

2005-05-25 04:36

Yes.

 

All I get is a blank page. The subreport has static text, and I can't even see that. I must be doing something terribly wrong.

 

 

 

 

By: new2jasper - villihelga

RE: Pass JRDataSource to SubReport

2005-05-25 04:54

I looked at the subform example, but, like all the examples, it uses a JDBC connection to get the data. I am not allowed to access the data directly (J2EE, WSAD, MVC crap)

 

 

 

 

By: Mitya - mityay

RE: Pass JRDataSource to SubReport

2005-05-25 05:08

define dataSourceExpression for subreport

 

 

 

 

By: Andreas Icecold - cubcuz

RE: Pass JRDataSource to SubReport

2005-05-25 05:14

so do what i told you, put your datasource ( type JRDataSource) as Parameter to your main report,

add the parameter as dataSourceExpression to the subreport Element nested in your main report.

 

in your subreport define a field to get access to the single records.

 

best you just test the subreport itself without the main report.

 

JasperDesign jasperDesignSub = JRXmlLoader.load("ODSPAcceptance.jrxml");

JasperCompileManager.compileReportToFile(jasperDesignSub, "ODSPAcceptance.jasper");

 

 

Map parameters = new HashMap();

parameters.put("Title", "ODSP Test Report");

parameters.put("ProgramName", "The Program Name");

...

 

JasperPrint jasperPrint =

JasperFillManager.fillReport(

jasperReportSub,

parameters,

new JRMapCollectionDataSource(Arrays.asList(dataMapArray()))

);

 

this should work.

 

with mainreport it would be like that:

 

JasperDesign jasperDesign = JRXmlLoader.load("ODSP001.jrxml");

JasperReport jasperReport =

JasperCompileManager.compileReport(jasperDesign);

 

JasperDesign jasperDesignSub = JRXmlLoader.load("ODSPAcceptance.jrxml");

JasperCompileManager.compileReportToFile(jasperDesignSub, "ODSPAcceptance.jasper");

 

 

 

 

// Map SubReport

Map paraSub = new HashMap();

paraSub.put("AcceptanceName","The sub map exceptance name");

paraSub.put("Test","The sub map test value");

 

// Map of parameters passed to the report.

Map parameters = new HashMap();

parameters.put("Title", "ODSP Test Report");

parameters.put("ProgramName", "The Program Name");

parameters.put("dataSourceSub", new JRMapCollectionDataSource(Arrays.asList(dataMapArray()))

);

 

JasperPrint jasperPrint =

JasperFillManager.fillReport(

jasperReport,

parameters,

new JREmptyDataSource());

 

 

 

 

 

 

By: Andreas Icecold - cubcuz

RE: Pass JRDataSource to SubReport

2005-05-24 06:49

Just use a JREmptyDataSource as datasource for the fillManager and put the datasource for your Subreport into the Parametermap of your masterreport,

then you can?t access to it via $P{parametername}

 

something like that:

Map parameters = new HashMap();

parameters.put("parametername",yourJRMapCollection);

 

asperFillManager.fillReport(

jasperReport,

parameters,

new JREmptyDataSource());

 

Hope i could help you.

 

 

 

 

By: Rafael Boschini - rboschinicps

RE: Pass JRDataSource to SubReport

2005-06-01 07:39

Hello,

 

My subreport iterate one time, in next register of master report the subreport not display but exister register for this subreport.

 

I send in parameters, one paremter DATA, DATA is JRBeanCollectionDataSource.

 

"

if (!m.isEmpty()){

JRDataSource dsDetail = new JRBeanCollectionDataSource(m);

map.put("DATA", dsDetail);

} else {

JREmptyDataSource dsDetail = new JREmptyDataSource();

map.put("DATA", dsDetail);

}

"

 

Somebody help me?

 

My subreport not iterate more time!

 

Sorry to my bad english

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

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