Jump to content
Changes to the Jaspersoft community edition download ×

To send argument to the scriptlet dynamically

2004 IR Help

Recommended Posts

By: veena - shanmugaveena

To send argument to the scriptlet dynamically

2005-03-02 22:43

Hai All,


I am new user of Jasper report. I want to export JfreeCharts to Pdf and Html format using JasperReports. Since My chart will be changing according to users requirements I want to convert the JFreechart of that requirement to pdf.


I following the same methodology in jasperReports-->demo-->sample-->Jfreechart.


Here instead of static pie chart i need to send my own jfreechart as argument to drawablerenderer class.I acheived it by having a constructor with string argument for the scriptlet and create an object in my own java class. Then i diretly call the compile,fill and convert to pdf methods of jasperreports. While doing so it is compiling good and creates a jasper file.But i cannt get the jrprint file and it throws a instantiated exception in class.


Since we are sending scriptlet as a variable in xml .How xml will take it while creating object to the scriptlet.


Thanks in advance.







By: W Leong - wk_leong

RE: To send argument to the scriptlet dynamically

2005-11-13 19:39



I am also looking at the same issue. Essentially, I have to pass parameter from


Servlet -to-> JasperReport -to-> JFreeChart


I know Servlet-to-JasperReport can be achieved via the parameters map. How do I continue passing it down to JFreeChart scriptlet?


Thank you for your time.




Link to comment
Share on other sites

  • 1 month later...
  • Replies 6
  • Created
  • Last Reply

Top Posters In This Topic

I'm facing a similar challenge:

I am querying a double from a datasource, storing it into a field.

I have written a scriptlet that accepts the float such as 123.45 and

returns "One Hundred Twenty Three and 45/100". In other words, supplying the

Double argument and returning the String for each record returned

from the datasource.


I'm getting the following error:

"Type mismatch: cannot convert from Double to int" at the point

where the .getValue() method is invoked on the double field.


First, is passing this field value to a scriptlet even supported?

Can't find any indication in the docs that it is.


Second, where is the convert to int being attempted and can it be cast

somehow in the call to circumvent the exception?

Link to comment
Share on other sites



Why do you say you face a similar challenge?

Your problem has nothing to do with passing values to the scriptlet.

The exception has to do only with the code you wrote in that scriptlet to convert the numeric value to its text representation.

It is not clear to me what code you have there as there is no getValue() method in the java.lang.Double class.


Seeing that code might help understand where the exception comes from.


Thank you,


Link to comment
Share on other sites

Thanks for your reply.


I just tried to send you a long, detailed response but the

session timed out and I lost all the text -- so, for now,

I'll just restate my first question: Is passing an argument

to a Scriptlet supported?


I'm willing to send you my code but I'm quite sure that the

getValue() method is called from the <serialization>.java

file generated from the .jrxml and derived from the

JRAbstractScriptlet class. Just not sure how.


Nevertheless, my code never actually gets called, as far as I

can determine.


If "forwarding" a datasource-derived field value through the

engine and back out to a scriptlet isn't supported, I'll just

implement it as a java stored procedure out of postgresql

and stop trying. Just wanted to know if it is doable and, if so,






Link to comment
Share on other sites

JasperReports expressions are Java expressions (given that you use a Java-based report compiler) that can contain placeholders for parameter, field and variable values. Therefore, anything you could write in a Java expression you can use in a JR expression, including calling methods on objects and passing arguments for the methods.


I'm not sure what exactly you are trying to do or what doesn't work. Maybe if you would post relevant code fragments (JRXML expressions, Java methods, field definitions, etc), we could figure out what goes wrong.




Link to comment
Share on other sites

// To give you a sense for how I've been approaching the jrxml part of this...


<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport name="dollars2string" columnCount="1" printOrder="Vertical" orientation="Portrait" pageWidth="595" pageHeight="842"

columnWidth="535" columnSpacing="0" leftMargin="30" rightMargin="30" topMargin="20" bottomMargin="20" whenNoDataType="NoPages"

scriptletClass="dollars2stringScriptlet" isTitleNewPage="false" isSummaryNewPage="false">




/** Then this is the syntax I've been using to invoke the scriptlet, where amt2pay is a Double successfully queried in from the datasource.

* I tried wrapping the argument in parens but that was disallowed at report compile time)



<textFieldExpression class="java.lang.String"><![CDATA[!CDATA[$P{REPORT_SCRIPTLET} [$F{amt2pay}]]]]></textFieldExpression>



// The complaint at compile time is this...

Type mismatch: cannot convert from Double to int

value = (java.lang.String)(!CDATA[((dollars2stringScriptlet)parameter_REPORT_SCRIPTLET.getValue()) [((java.lang.Double)field_amt2pay.getValue())]]);


/** I did a little hunting for where the getValue() method is invoked and it appears to be in JRAbstractScriptlet which is extended by

* my Scriptlet, via JRDefaultScriptlet,

*/ as follows...


// My code:

public class dollars2stringScriptlet extends net.sf.jasperreports.engine.JRDefaultScriptlet {

public void beforeReportInit() throws JRScriptletException









//Relevant (?) net.sf.jasperreports.engine.JRAbstractScriptlet code

public Object getFieldValue(String fieldName) throws JRScriptletException


JRFillField field = (JRFillField)this.fieldsMap.get(fieldName);

if (field == null)


throw new JRScriptletException("Field not found : " + fieldName);


return field.getValue();



/** My guess is that the exception is thrown at the time amt2pay (the Double value successfully fetched from the database and determined to

* be of type Double) is then used as the argument to the getFieldValue method. That the JRScriptletException isn't thrown suggests things

* go pretty well to that point but, somehow, fail on the field.getValue() call which must, either by explicit declaration or default, expect a

* type int. This is where I lose the thread. Too many inconsistencies. Thus my posting as to supportability and, if supported, syntax.


Link to comment
Share on other sites

Correct me if I'm wrong, but your expression is



Ignoring the !CDATA[...] parts which have no reason to be here, the expression is $P{REPORT_SCRIPTLET} [$F{amt2pay}] which would only amount to a valid Java expression if $P{REPORT_SCRIPTLET} would be an array and $F{amt2pay} would resolve to a primitive int value. But since $P{REPORT_SCRIPTLET} is not an array but a regular object instance and $F{amt2pay} is a java.lang.Double object, this expression is far from being valid.


What exactly are you trying to achieve with this expression? By looking at it, it doesn't make any sense..




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