Passing parameters to a Jasper report

来源:互联网 发布:3d构图软件 编辑:程序博客网 时间:2024/05/16 15:33

 

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

tobysaville
May 10th, 2006, 08:46 AM
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("report.name", model);


hope that helps

cheers, tobes

Wojciech Bernacki
May 10th, 2006, 09:30 AM
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

Wojciech Bernacki
May 10th, 2006, 10:05 AM
Problem solved, silly mistake :)
Anyway, thanks for help!

Wojtek

adeveloper
May 15th, 2006, 12:41 AM
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>

tobysaville
May 15th, 2006, 04:15 AM
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;
}

adeveloper
May 15th, 2006, 07:47 PM
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.

anarinsky
Sep 30th, 2006, 11:39 PM
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.ResourceBundl eViewResolver">
<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?

tobysaville
Oct 4th, 2006, 09:08 AM
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.

anarinsky
Oct 4th, 2006, 11:31 AM
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
原创粉丝点击