Scriptlets allow you to execute snippets of Java code (or other language) during the filling process.
Your custom Scriptlet class must extend either JRDefaultScriptlet or JRAbstractScriptlet, and override these methods:
beforeReportInit,
afterReportInit,
beforePageInit
...
The name of the methods also show when the methods will be invoked.
Here is an example.
TrackingReportScriptlet.java
package com.db.report.jasper.custom.trackingreport;
import java.sql.Connection;
import java.sql.SQLException;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import org.apache.log4j.Logger;
import com.db.misportal.util.ConnectionPool;
import com.db.report.jasper.util.DateTool;
import net.sf.jasperreports.engine.JRScriptletException;
public class TrackingReportScriptlet extends JRDefaultScriptlet {
public void beforeReportInit() throws JRScriptletException
{
//Invoked before the report is initilized
}
public void afterReportInit() throws JRScriptletException
{
//Invoked after the report is initilized
}
public void beforePageInit() throws JRScriptletException
{
// Invoked before each page is initilized
}
public void afterPageInit() throws JRScriptletException
{
// Invoked after each page is initilized
}
public void beforeColumnInit() throws JRScriptletException
{
// Invoked before each column is initilized
}
public void afterColumnInit() throws JRScriptletException
{
// Invoked after each column is initilized
}
public void beforeGroupInit(String groupName) throws JRScriptletException
{
// Invoked before the group named "groupName" is initilized
if ("group1".equals(groupName))
{
// do something
}
else if ("group2".equals(groupName))
{
// do something else
}
}
public void afterGroupInit(String groupName) throws JRScriptletException
{
// Invoked after the group named "groupName" is initilized
/*
if ("group1".equals(groupName))
{
// do something
}
else if ("group2".equals(groupName))
{
// do something else
}
*/
}
public void beforeDetailEval() throws JRScriptletException
{
// Invoked before the detail section is evaluated.
}
public void afterDetailEval() throws JRScriptletException
{
// Invoked after the detail section is evaluated.
String strReportDate = (String)getParameterValue("PARAM_REPORT_DATE");
String strReportDateLastYear = DateTool.getLastYear(strReportDate);
_log.debug("last year = "+(String)getVariableValue("VAR_REPORT_DATE_LAST_YEAR"));
setVariableValue("VAR_REPORT_DATE_LAST_YEAR",strReportDateLastYear);
_log.debug("last year = "+strReportDateLastYear);
}
private static Logger _log = Logger.getLogger(TrackingReportScriptlet.class);
}
After writting the scriptlet class, you need to set the scriptletClass atribute to the fully qualified name of the scriptlet class. For example,
< jasperReport
name="tracking_part5"
columnCount="1"
printOrder="Vertical"
orientation="Landscape"
pageWidth="782"
pageHeight="595"
columnWidth="782"
columnSpacing="0"
leftMargin="0"
rightMargin="0"
topMargin="0"
bottomMargin="0"
whenNoDataType="NoPages"
scriptletClass="com.db.report.jasper.custom.trackingreport.TrackingReportScriptlet" isTitleNewPage="false"
isSummaryNewPage="false">
That's all !
December 02, 2006
How to use Scriptlets
Tags:
JasperReports
Subscribe to:
Post Comments (Atom)
1 responses:
when i use afterDetailEval(), i found that it is been executed twice??
do u have an explanation?
Post a Comment