Announcement Announcement Module
No announcement yet.
Passing parameters to a Jasper report Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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:
    Map model = new HashMap();
    model.put("paramName", paramValue);
    return new ModelAndView("", model);
    hope that helps

    cheers, tobes


    • #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"/>
      <font size="14"/>
      <textFieldExpression class="java.lang.String">

      Any ideas?



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



        • #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();
          return beanData;

          I get the following Exception:

          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(
          at net.sf.jasperreports.engine.fill.JRFillDataset.set FillParameterValues(
          at net.sf.jasperreports.engine.fill.JRFillDataset.set ParameterValues(
          at net.sf.jasperreports.engine.fill.JRBaseFiller.setP arameters(
          at net.sf.jasperreports.engine.fill.JRBaseFiller.fill (
          at net.sf.jasperreports.engine.fill.JRBaseFiller.fill (
          at net.sf.jasperreports.engine.fill.JRFiller.fillRepo rt(
          at net.sf.jasperreports.engine.JasperFillManager.fill Report(
          at org.springframework.web.servlet.view.jasperreports .AbstractJasperReportsView.fillReport(AbstractJasp
          at org.springframework.web.servlet.view.jasperreports .AbstractJasperReportsView.renderMergedOutputModel (
          at org.springframework.web.servlet.view.AbstractView. render(
          at org.springframework.web.servlet.DispatcherServlet. render(
          at org.springframework.web.servlet.DispatcherServlet. doDispatch(
          at org.springframework.web.servlet.DispatcherServlet. doService(
          at org.springframework.web.servlet.FrameworkServlet.p rocessRequest(
          at org.springframework.web.servlet.FrameworkServlet.d oGet(
          at javax.servlet.http.HttpServlet.service(HttpServlet .java:743)
          at javax.servlet.http.HttpServlet.service(HttpServlet .java:856)


          • #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:
            private Map getModel() {
              Map model = new HashMap();
              Collection beanData = getBeanData();
              model.put("dataSource", beanData);
              return model;


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


              • #8
                Which file should be specified as URL in 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 as a source code:
                For example,

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

                simpleReport.class=org.springframework.web.servlet .view.jasperreports.JasperReportsHtmlView

                However, all examples show that 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?


                • #9

                  if you set the property


                  Then your jrxml will be compiled on the fly.

                  Otherwise, point your url to the .jasper file.


                  • #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 [] 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