Announcement Announcement Module
Collapse
No announcement yet.
Passing parameters to a Jasper report Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing parameters to a Jasper report

    Is there any way I can pass an arbitrary report parameter to a Jasper report, so that it would be accessible via standard ${parameterName} syntax? I am using Spring 1.2.8

    Wojciech Bernacki

  • #2
    You cant just use ${paramName} in the jasper report's JRXML file, if thats what you were refering to, you need to use $P{paramName}

    In order to pass a param to a report in Spring (given that you have configured the report as a spring view) is as simple as adding the values to a model Map object which is passed to the ModelAndView object:
    Code:
    Map model = new HashMap();
    model.put("paramName", paramValue);
    return new ModelAndView("report.name", model);
    hope that helps

    cheers, tobes

    Comment


    • #3
      Hi Tobes,
      thanks for reply...

      That is the way I try to pass a param:

      ...
      modelMap.put("userKey", "test");
      return new ModelAndView(TEST_REPORT, modelMap);
      ...

      Then, in the report file I declare a corresponding Jasper parameter:
      <parameter name="userKey" class="java.lang.String"/>

      Unfortunetely, although everything compiles well and runs without any errors or warnings, I get no output in the generated PDF:

      <textField evaluationTime="Report">
      <reportElement x="350" y="10" width="75" height="20"/>
      <textElement>
      <font size="14"/>
      </textElement>
      <textFieldExpression class="java.lang.String">
      $P{userKey}
      </textFieldExpression>
      </textField>

      Any ideas?

      Wojtek

      Comment


      • #4
        Problem solved, silly mistake
        Anyway, thanks for help!

        Wojtek

        Comment


        • #5
          What was the problem.

          I am having problem using jasper reports with spring. I am using spring 1.2.7.

          My setup is as follows:
          //in .jrxml file: (iReport 1.2.2) & jasper reports 1.2.2
          <parameter name="myBeanData" isForPrompting="false" class="java.lang.String"/>

          //in controller
          return new ModelAndView("sampleJasperReport", getModel());

          private Map getModel() {
          Map model = new HashMap();
          Collection beanData = getBeanData();
          model.put("myBeanData", beanData);
          return model;
          }

          private Collection getBeanData() {
          Collection beanData = new ArrayList();
          beanData.add("First");
          beanData.add("Second");
          return beanData;
          }

          ------------------
          I get the following Exception:

          <code>
          net.sf.jasperreports.engine.JRException: Incompatible java.util.ArrayList value assigned to parameter myBeanData in the sampleJasperReport dataset.
          at net.sf.jasperreports.engine.fill.JRFillDataset.set Parameter(JRFillDataset.java:782)
          at net.sf.jasperreports.engine.fill.JRFillDataset.set FillParameterValues(JRFillDataset.java:607)
          at net.sf.jasperreports.engine.fill.JRFillDataset.set ParameterValues(JRFillDataset.java:563)
          at net.sf.jasperreports.engine.fill.JRBaseFiller.setP arameters(JRBaseFiller.java:874)
          at net.sf.jasperreports.engine.fill.JRBaseFiller.fill (JRBaseFiller.java:689)
          at net.sf.jasperreports.engine.fill.JRBaseFiller.fill (JRBaseFiller.java:666)
          at net.sf.jasperreports.engine.fill.JRFiller.fillRepo rt(JRFiller.java:89)
          at net.sf.jasperreports.engine.JasperFillManager.fill Report(JasperFillManager.java:601)
          at org.springframework.web.servlet.view.jasperreports .AbstractJasperReportsView.fillReport(AbstractJasp erReportsView.java:586)
          at org.springframework.web.servlet.view.jasperreports .AbstractJasperReportsView.renderMergedOutputModel (AbstractJasperReportsView.java:527)
          at org.springframework.web.servlet.view.AbstractView. render(AbstractView.java:250)
          at org.springframework.web.servlet.DispatcherServlet. render(DispatcherServlet.java:965)
          at org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:744)
          at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:663)
          at org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:394)
          at org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:348)
          at javax.servlet.http.HttpServlet.service(HttpServlet .java:743)
          at javax.servlet.http.HttpServlet.service(HttpServlet .java:856)
          </code>

          Comment


          • #6
            Incompatible java.util.ArrayList value assigned to parameter myBeanData in the sampleJasperReport dataset
            This means you cannot assign an ArrayList to the parameter named myBeanData.

            The data that you want to loop over has to be assigned to the special parameter name "dataSource". So you want to do the following:
            Code:
            private Map getModel() {
              Map model = new HashMap();
              Collection beanData = getBeanData();
              model.put("dataSource", beanData);
              return model;
            }

            Comment


            • #7
              Thanks tobysaville for the reply.

              One of the main mistakes I made was instead of using Field I was using Parameter.

              I found out that it is not necessary to name the datasource as 'dataSource' as long as getBeanData() returns an instance of the following data type:
              JRDataSource.class, JRDataSourceProvider.class, Collection.class, Object[].class

              It is so difficult to get a working end-to-end program with Jasper and Spring. Jasper demo should be included in the spring distribution.

              Comment


              • #8
                Which file should be specified as URL in views.properties: Jrxml or jasper

                I just started working with Jasper reports, and this thing particularly is not clear for me.

                It seems logical that a jrxml file should be in view.properties as a source code:
                For example,

                <bean id="viewResolver" class="org.springframework.web.servlet.view.Resour ceBundleViewResolver">
                <property name="basename">
                <value>views</value>
                </property>
                </bean>

                views.properties:
                simpleReport.class=org.springframework.web.servlet .view.jasperreports.JasperReportsHtmlView
                simpleReport.url=/jasper/simpleReport.jrxml

                However, all examples show that views.properties specify a Jasper file in url as a source code. If a Jasper file is specified how a Jrxml file is translated to a Jasper file?

                Comment


                • #9
                  Hello,

                  if you set the property

                  simpleReport.reportCompiler=net.sf.jasperreports.e ngine.design.JRJdtCompiler

                  Then your jrxml will be compiled on the fly.

                  Otherwise, point your url to the .jasper file.

                  Comment


                  • #10
                    Thank you for the response.

                    As I found one can specify the jrxml extension and conversion to PDF works fine. However, any other extension results in error.

                    I thought that by specifying a compiler we can use other extension. However, the suggested compiler specification resulted in the following error:

                    org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [net.sf.jasperreports.engine.design.JRCompiler] for property 'reportCompiler'; nested exception is java.lang.IllegalArgumentException: No matching editors or conversion strategy found
                    Caused by:
                    java.lang.IllegalArgumentException: No matching editors or conversion strategy found

                    Comment

                    Working...
                    X