Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing methods in jasper report?

I am creating a jasper report.In that I want to write one method which takes integer and does some process and returns a string.I dont know how to write methods in jasper report.Is it possible to write?Can any one help me in this

I am using iReport3.6.0.

Sample code :

 <textField>
  <reportElement x="400" y="10" width="80" height="15"/>
  <textElement textAlignment="Left" verticalAlignment="Middle"/>
  <textFieldExpression     class="java.lang.String">
               <![CDATA[$F{intValue}]]>
  </textFieldExpression>
 </textField>

In the above code "$F{intValue}" returns integer.I want pass that to one method and that methods return type wants to be string.

Thanks

like image 660
DonX Avatar asked Sep 24 '09 08:09

DonX


People also ask

How do you write expressions in JasperReports?

JasperReports doesn't support if-else statements when defining variable expressions. Instead, you can use the ternary operators {cond} ? {statement 1} : {statement 2}. This operator can be nested inside a Java expression to obtain the desired output based on multiple conditions.

How do you write if condition in Jasper report?

Jasper Reports doesn't support if-else statements when defining variable expressions. Instead you can use the ternary operators {cond} ? {statement 1} : {statement 2}. You can nest this operator inside a Java expression to obtain the desired output based on multiple conditions.

What language does jaspersoft use?

All of the formulas in JasperReports are defined through expressions. The default expression language is Java, but if you are not a programmer, we recommend that you design your projects with JavaScript or Groovy because those languages hide a lot of the Java complexity.


2 Answers

Write a helper Java class with a static method that will receive the integer argument and return desired outcome:

package com.yourname.reports.util;

public class JrUtils {
  public static String intFormatter(int arg) {
    return "Beautified int: " + arg;
  }
}

Add this class to the classpath used for compiling jasperreports template and for the runtime. In the iReport right click on report's title in 'Report Inspector' view and choose 'Properties'. Scroll down to 'Imports' and add your class:

com.yourname.reports.util.JrUtils

Add import Java class to your report and invoke the static method from the field using:

<![CDATA["Transformed int: " + JrUtils.intFormatter($F{intValue}) ]>
like image 54
Boris Pavlović Avatar answered Sep 27 '22 16:09

Boris Pavlović


@Boris Pavlović answer is good, but I think it miss one little think- classpath. So If You have an error on compile like:

net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: 
Only a type can be imported. com.core.report.Util resolves to a package import com.core.report.Util; 

. Util cannot be resolved value = (java.lang.String)(Util.doit(((java.sql.Timestamp)field_time.getValue())));

You have to add *.jar of You project which contains declared helper class as follow:

> In You iReport Designer go to Tool -> Options -> iReport -> Classpath -> 
and press button "Add JAR" and select You project's jar.
like image 41
masterdany88 Avatar answered Sep 27 '22 15:09

masterdany88