Jump to content

Using JRBeanCollectionDataSource in Subreports


kunkasurendra

Recommended Posts

Hi,

 

I want design the subreports using JRBeanCollectionDataSource,I am able to display the single report,but i am not able to display the subreports using JRBeanCollectionDataSource.Please help me,any one worked on the bean subreports,if you have any samples please send me with java examaple.

 

 

 

 

Thanks in Advance,

Surendra Babu.K.

Post edited by: kunkasurendra, at: 2008/01/24 10:04

Link to comment
Share on other sites

  • 1 year later...
  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

 Bump.

 

I have a report that uses JRBeanCollectionDataSource and it works fine, But when I Use JRBeanCollectionDataSource for the sub report, all my fields show up as null.  If I run the subreport.jasper by itself, the data shows fine, but when run as a subreport, it doesn't work.  Code Snippets are below:

 

Please Help!

 

 

 

Code:
The Sub Report Declaration in my Main Report:			<subreport isUsingCache="true">				<reportElement x="0" y="0" width="200" height="100"/>				<dataSourceExpression><![CDATA[$P{DATA}]]></dataSourceExpression>				<subreportExpression class="java.lang.String"><![CDATA[$P{PAGE1}]]></subreportExpression>			</subreport>Java Code Calling Main Report:File masterFile = new File(getServlet().getServletContext().getRealPath("/") + "report.jasper");        File sub1File = new File(getServlet().getServletContext().getRealPath("/") + "subReport.jasper");        JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(factory.getData());        JasperReport masterReport = (JasperReport) JRLoader.loadObject(masterFile.getPath());        Map parameters = new HashMap();        parameters.put("PAGE1", sub1File.getPath());        parameters.put("DATA", ds); // passing in the bean for use in subReport        JasperPrint jp = JasperFillManager.fillReport(masterReport, parameters, ds);        JRPdfExporter exporter = new JRPdfExporter();        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);        ByteArrayOutputStream baos = new ByteArrayOutputStream();        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);        try {            exporter.exportReport();        } catch (JRException e) {            throw new ServletException(e);        }// snip -- here baos should be full of data, but it is empty because // the sub-report cannot see the data passed in parameters.get("DATA");
Link to comment
Share on other sites

 I've even tried the following to use the Parent's Data Source to no avail:

Code:
			<subreport isUsingCache="true">				<reportElement x="0" y="0" width="200" height="100"/>				<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>				<subreportExpression class="java.lang.String"><![CDATA[$P{PAGE1}]]></subreportExpression>			</subreport>
Link to comment
Share on other sites

The answer is here:  http://www.rodiq.ro/2008/01/26/subreports-in-jasper-without-sql/

 

Below is the changes you need to make to get this to work.

Code:
In the master .jasper file:    <subreport isUsingCache="true">        <reportElement x="0" y="0" width="200" height="100"/>        <dataSourceExpression><![CDATA[$P{DATA}]]></dataSourceExpression>        <subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[$P{PAGE1}]]></subreportExpression>    </subreport>In the Java File:        MyDataFactory factory = new MyDataFactory();        File masterFile = new File(getServlet().getServletContext().getRealPath("/") + "reports/annual/annualReport.jasper");        File sub1File = new File(getServlet().getServletContext().getRealPath("/") + "reports/annual/page1.jasper");        Collection beanCollection = factory.getData(); // the collection of beans with the data        JasperReport masterReport = (JasperReport) JRLoader.loadObject(masterFile.getPath());        JasperReport subReport = (JasperReport) JRLoader.loadObject(sub1File.getPath());        Map parameters = new HashMap();        parameters.put("PAGE1", subReport);        parameters.put("DATA",  new JRBeanCollectionDataSource(beanCollection));        JasperPrint jp = JasperFillManager.fillReport(masterReport, parameters,  new JRBeanCollectionDataSource(beanCollection));        JRPdfExporter exporter = new JRPdfExporter();        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);        ByteArrayOutputStream baos = new ByteArrayOutputStream();        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
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...