Announcement Announcement Module
Collapse
No announcement yet.
Jasper Report: pass more than one data source Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Jasper Report: pass more than one data source

    Rob,

    Please do help with this issue. I have been struggling and learning jasper report with spring 1.13 for a while with a serious project. Now I got the basic idea how to deal with them (along with Jasper Assistant under eclipse). I also made several demos work. Thanks for the wonderful work.

    Now I can only pass one list(or data source) to report. But for some reports, I have more than one lists. It seems I have to use subreport, but then I don't know how to pass my lists to both parent report AND subreport. It is fine if I only pass one list, both parent report or subreport can get it.

    My questions are:

    1) if I can really pass more than one list to report. I tried a couple of ways, but I got: More than one value of type [java.util.Collection] found exception, or Error retrieving field value from bean
    2) is subreport the only way to loop through more than one data source, or there is another better ways to do.

    I think it is very common to have more than one list in a report, and I just wonder if spring 1.1.3 currently support it.

    Best Regards,

    Thank you!

  • #2
    closely watch the reply

    Hi,

    I would very like to see if anyone could throw some ideas with this issue. Any info would be highly appreciated. Thanks.

    Comment


    • #3
      Currently, there is no support for multiple data sources in the Spring 1.1.3 release of JasperReports support. I'll take a look at adding it in tomorrow - hopefully it will make it in ready for 1.1.4

      Rob

      Comment


      • #4
        After looking into this in a little more detail, it should be possible to use subreports with the 1.1.3 support classes - it's just not that nice .

        Essentially you simply add the additional JRDataSource instance, under the appropriate key and the JasperReport instance, under the appropriate key, to the model returned from your controller. These parameters are then passed directly into the Jasper engine for reporting rendering.

        In 1.1.4, I hope to add support for subreports that does not couple your views so closely to JR when using subreports.

        Rob

        Comment


        • #5
          need a simple example

          Thank you very much for the reply, Rob. It is very nice to hear back from you.

          I am not quite sure if I absorbed what you have said, but I will give it a try with spring 1.1.3.

          First of all, my understanding is that we have to use subreport if I have more than one datasource, right? It seems most the Jasper demos only have one datasource in detail band. The only demo which has more than one datasource is using subreport. Please correct me if I am wrong.

          It would be nice if you can give a simple demo for how to do this under 1.1.3. What I am doing is really straight forward. In my controller, I have a map myModel which is returned to *.japser (or *.jrxml) view. In this myModel, I just put all the necessary parameters and fields in it, such as:



          Code:
                          //myModel is an instance of HashMap and data is an ArryList
          	myModel.put("paraName", "this is a para");
          
          	TestTryVo testVo1 = new TestTryVo();
          	testVo1.setAdvantage("advantage1");
          	data.add(testVo1);
          
          	TestTryVo testVo2 = new TestTryVo();
          	testVo2.setAdvantage("advantage2");
          	testVo2.setRowGroup("rowGroup2");
          	data.add(testVo2);
          
          // Here I have another ArrayList data2 to report, but I can't make it work
          //		data2.add(another Vo);
          
          	myModel.put("reportDataKey", data);
          //  I can't add another ArrayList as below:
          //	myModel.put("reportDataKey_2", data2);
          
          	return new ModelAndView("myReportView", myModel);
          In my *.jasper, I can find only one list when I use $F(attribute in TestTryVo) in both parent report or subreport.

          If your time is limited, please give an example only when 1.1.4 comes is born. So we can follow the sample instead of trying in the darkness.

          Thank you and spring team for the great work!

          Comment


          • #6
            I just tried to put together an example using the current view classes, when I realised that it won't work since the view classes are only equipped to handle one DataSource. You can get around this by using direct queries for your subreports but this isn't so good.

            I'm back at work next week, holiday time is over , I will take a look at extending the current classes to support subreports in some form.

            Rob

            Comment


            • #7
              I can't pass parameters either

              Thanks again for the reply. For now, I think to have query in subreport may be the only way to retrieve more data resource. But I have another problem, since the query generally are prepared statements, this means I need pass parameters from master report or from controller. But I can't get parameter value by using $P{a parameter name from model} in subreport, but instead I can get field like $F{...}. Did I do something wrong or that is the way it currently is?

              I may have to wait for you guys to have a better solution for multiple datasources in next Spring release (with more examples 8) . I should say it is common to have more than one datasource in real project. Otherwise we have to go back to iText or JFree Chart (still with spring of course).

              Regards,

              Comment


              • #8
                All,

                I just committed the initial code for sub-report support into CVS along with a new sample application that demonstrates the JasperReports view classes in action.

                Please post any bugs or suggestions on JIRA and remember: this isn't released yet so it may change .

                Rob

                Comment


                • #9
                  couldn't find any report example

                  Hi Rob,

                  I just downloaded the code but I couldn't find anything with jasper report example. Maybe I am checking the wrong one? The one I downloaded is:

                  spring-framework-1.1.4-dev-with-dependencies-build.185-20050112.zip

                  from http://www.springframework.org/downloads/nightly/ (Nightly Snapshot Builds).

                  Could you provide more info regards where I can find your example? Thanks!

                  Comment


                  • #10
                    http://cvs.sourceforge.net/viewcvs.p...es/jasperdemo/
                    I do not know if it is included in the nightly build.
                    HTH

                    Comment


                    • #11
                      made demo work and will look into further.

                      I just made Rob's demo work and will look into and learn it further. Talk to you later.

                      Thanks Rob and Omar

                      Comment


                      • #12
                        jasper reports demo questions/issues

                        all,

                        I post my concerns in JIRA. For those who wants see my issues here, I copy and paste it below:
                        For Rob Harrop's new jasper demo with subreport:

                        I downloaded the source from CVS and made it work. Glad to see there are some improvements.

                        Here are several my questions/suggestions:

                        1) the demo only has a subReportParent.jrxml with one subReportChild.jrxml. I would like to know if we can have more than one sub report to config in jrtest-servlet.xml. The means one master report with more than one subreports.

                        2) in the demo, the sub list ("SubReportData") only loops once for the first item (city: Manchester) in parent list "dataSource". My understanding is that it should loops for each of the item in parent list. In this demo, sub datasource ("SubReportData") should loop for "Manchester", then under "London", then "Manchester" again, then "London", ...

                        Is this all right or I misunderstood?

                        3) another small issue is that "subReport" view in ReportController.java didn't specified in views.properties, instead it is in jrtest-servlet.xml. But all the other views are defined in views.properties. This is a bit inconsistent.

                        I think No. 3) is not a big issue and I can stay with it. Please clarify 1) and 2).

                        Again, thank you very much and look forward to hearing from you!

                        Comment


                        • #13
                          In response to your queries:

                          1) You can have multiple subreports by adding multiple entries to the subReports property.

                          2) I expected the same behaviour as you, but that is the behaviour exhibited when using a JRDataSource for a subreport. The subreport samples included in the JasperReports distribution use direct queries for subreports and this query is executed once per iteration into the subreport. I guess a JRDataSource is useful for subreports that don't need to be used per iteration. You can still configure subreports without passing in data sources for these subreports. I am hoping to see better support for JRDataSources in subreports in later releases of JasperReports. After all we are still on 0.6.3

                          3) This was just for illustration. You can configure the views how you like. However, in order to specify the map for subreports you will need to use an XML configuration approach rather than a properties file.

                          Regards,

                          Rob

                          Comment


                          • #14
                            1) it is good to have multiple subreports, each subreport has a list. So I can generate a master report which has more than one lists.

                            My question is that how I can specify more subreport datasource in jrtest-servlet.xml:

                            now with one parent and one child, it is like:

                            <bean id="subReport" class="org.springframework.web.servlet.view.jasper reports.JasperReportsPdfView">
                            <property name="url">
                            <value>/WEB-INF/reports/subReportParent.jrxml</value>
                            </property>
                            <property name="subReports">
                            <map>
                            <entry key="ProductsSubReport">
                            <value>/WEB-INF/reports/subReportChild.jrxml</value>
                            </entry>
                            </map>
                            </property>
                            <property name="subReportDataSources">
                            <value>SubReportData</value>
                            </property>
                            <property name="reportDataKey">
                            <value>dataSource</value>
                            </property>
                            </bean>
                            if I have one more, should it like:

                            <bean id="subReport" class="org.springframework.web.servlet.view.jasper reports.JasperReportsPdfView">
                            <property name="url">
                            <value>/WEB-INF/reports/subReportParent.jrxml</value>
                            </property>
                            <property name="subReports">
                            <map>
                            <entry key="ProductsSubReport">
                            <value>/WEB-INF/reports/subReportChild.jrxml</value>
                            </entry>
                            <entry key="anotherSubReport">
                            <value>/WEB-INF/reports/subReportChild2.jrxml</value>
                            </entry>
                            </map>
                            </property>
                            <property name="subReportDataSources">
                            <value>SubReportData</value>
                            </property>
                            <property name="subReportDataSources">
                            <value>SubReportData2</value>
                            </property>
                            <property name="reportDataKey">
                            <value>dataSource</value>
                            </property>
                            </bean>
                            It doesn't look right without know which data source is for which subreport. Please give some explaination to the above bean definition.

                            2) I actually don't need to loop a list per iteration (say this like a doulbe embedded for loop). For now, as long as I can use multiple lists, each list can loop through ITSELF, it should be enough.

                            again thanks for the time and reply. I am looking forwading for the spring 1.1.4 release which has this.

                            I will report to you anything I enounter when I actually do it in the near future.

                            Regards,

                            Comment


                            • #15
                              or it maybe:
                              <bean id="subReport" class="org.springframework.web.servlet.view.jasper reports.JasperReportsPdfView">
                              <property name="url">
                              <value>/WEB-INF/reports/subReportParent.jrxml</value>
                              </property>
                              <property name="subReports">
                              <map>
                              <entry key="ProductsSubReport">
                              <value>/WEB-INF/reports/subReportChild.jrxml</value>
                              </entry>
                              <entry key="anotherSubReport">
                              <value>/WEB-INF/reports/subReportChild2.jrxml</value>
                              </entry>
                              </map>
                              </property>
                              <property name="subReportDataSources">
                              <value>SubReportData</value>
                              <value>SubReportData2</value>
                              </property>
                              <property name="reportDataKey">
                              <value>dataSource</value>
                              </property>
                              </bean>
                              a key "subReportDataSources" will have two values (SubReportData SubReportData2)? This is even worse? :?

                              Comment

                              Working...
                              X