Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing/importing user defined classes in jrxml

Have anyone tried to import user defined classes in jasper report (.jrxml file)? I want to use some (user defined) Util class inside my jasper report to cook some bean attributes. I am using Javabean datasource

Please let me know if you need further clarification.

syntax to import class is

<import value="java.util.HashMap"/>

I want to use

<import value="mypackage.MyUtil" />
 ....
 ....
<field name="myVar" class="java.lang.String">
    <fieldDescription><![CDATA[MyUtil.cook(myData)]]>
    </fieldDescription>
</field>

The simple definition for MyUtil.java could be

package mypackage;
public class MyUtil
{
    public static String cook(String data)
    {
        return data + "_cooked";
    }
}
like image 926
Nayn Avatar asked Dec 22 '22 07:12

Nayn


2 Answers

I think I should have tried sufficiently before asking this.

There is nothing extra needed apart from There are two sections in jrxml: 1. Defining fields from javabean source 2. Using fields defined in step 1. to populate values in detail band

I was trying to cook the value of javabean members even before they are used to create fields So, jasper was trying to parse that 'expression' as javabean member.

Following is wrong

<field name="myVar" class="java.lang.String">
     <fieldDescription><![CDATA[MyUtil.cook(myData)]]>
     </fieldDescription>
</field>

When I used the Util class on field value, it worked.

<textField>
    <reportElement x="200" y="0" width="100" height="13"/>
    <textElement/>
    <textFieldExpression class="java.lang.String">
         <![CDATA[MyUtil.cook($F{myVar})]]>
    </textFieldExpression>
</textField>

Thanks Nayn

like image 183
Nayn Avatar answered Jan 22 '23 12:01

Nayn


You have to set the classpath in your iReport. It depends on its version, but is generally under Settings/Classpath

like image 36
Bozho Avatar answered Jan 22 '23 10:01

Bozho