JasperReport初级教程(2.0.3)调用Java代码_图文详解_绝对原创
来源:互联网 发布:兰州辉煌软件 编辑:程序博客网 时间:2024/06/05 04:41
初级入门请查看:
JasperReport初级教程(2.0.3)图文详解_绝对原创
如需转载,请注明出处,http://www.川江号子.cn 。
另,本人有Word电子文档格式,如需要,请联系本人:asima127@gmail.com
由于有些图片以前是放在巴巴变上的,现在巴巴变不提供外链了,如果你愿意支付一定的费用,请访问该文档的购买地址:【http://item.taobao.com/auction/item_detail-0db2-03c1768339f915aec75afd6f1f49426b.jhtml】
1 iReport中调用Java代码
在iReport中调用Java代码目前使用其内置的一个Scriptlet编辑器来完成。它会产生对应的class文件。
1.1 编辑Scriptlet代码
在工具栏中找到“编辑”,然后选择“Scriptlet编辑器”,如图:
会提示如下信息,点击“是(Y)”继续
出现如下编辑界面:
在Events部分,下拉可以看到很多事件,这里就保持第一项“<imports and global declarations>”即可,如有需要再另行选择。
其中,需要修改<ScriptletClassName>,修改必须为“iReport文件名+Scriptlet”,比如这个iReport保存的名称为“MyJavaIReport.jrxml”,这里就需要修改为“MyJavaIReportScriptlet”,因为这个Scriptlet内容编译后默认会被保存为“MyJavaIReportScriptlet.java”文件。
编写一个test方法,内容如下,完成后,点击“save”保存:
1.2 生成Java文件
这一步不是必须,完全可以自己来写这个java类,只要是继承IReportScriptlet类就可以了。
更改报表属性,在工具栏中选择“编辑”,再选择“报表属性”
如上图,选择“Scriptlet class”,然后在下拉列表中选择“Use iReport internal scriptlet support”,保持Language为java,这样,编译的时候就会产生Java文件了。
设置完成后,点击OK,然后点 中的第一个进行编译,这样就可以在jrxml文件的同一目录产生MyJavaIReportScriptlet.java文件。这里会出现错误,没有关系,这一步主要要产生这个java文件。错误如图:
再检查一下对应的jrxml文件所在目录下是否生成了java文件,
以下为java文件内容,可以看到编辑的方法和一些Event事件代码:
import net.sf.jasperreports.engine.*;
public class MyJavaIReportScriptlet extends it.businesslogic.ireport.IReportScriptlet ...{
/** *//** Creates a new instance of JRIreportDefaultScriptlet */
public MyJavaIReportScriptlet() ...{
}
public String test()...{
return "hello";
}
/** *//** Begin EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */
public void afterColumnInit() throws JRScriptletException
...{
super.beforeColumnInit();
}
/** *//** End EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */
public void afterDetailEval() throws JRScriptletException
...{
super.afterDetailEval();
}
/** *//** End EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't modify or move please! */
public void afterGroupInit(String groupName) throws JRScriptletException
...{
super.afterGroupInit(groupName);
}
/** *//** End EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't modify or move please! */
public void afterPageInit() throws JRScriptletException
...{
super.afterPageInit();
}
/** *//** End EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't modify or move please! */
public void afterReportInit() throws JRScriptletException
...{
}
/** *//** End EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */
public void beforeColumnInit() throws JRScriptletException
...{
}
/** *//** End EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */
public void beforeDetailEval() throws JRScriptletException
...{
}
/** *//** end EVENT_BEFORE_DETAIL_EVAL Please don't touch or move this comment*/
/** *//** End EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't modify or move please! */
public void beforeGroupInit(String groupName) throws JRScriptletException
...{
}
/** *//** End EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't modify or move please! */
public void beforePageInit() throws JRScriptletException
...{
}
/** *//** End EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_BEFORE_REPORT_INIT This line is generated by iReport. Don't modify or move please! */
public void beforeReportInit() throws JRScriptletException
...{
}
/** *//** End EVENT_BEFORE_REPORT_INIT This line is generated by iReport. Don't modify or move please! */
}
public class MyJavaIReportScriptlet extends it.businesslogic.ireport.IReportScriptlet ...{
/** *//** Creates a new instance of JRIreportDefaultScriptlet */
public MyJavaIReportScriptlet() ...{
}
public String test()...{
return "hello";
}
/** *//** Begin EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */
public void afterColumnInit() throws JRScriptletException
...{
super.beforeColumnInit();
}
/** *//** End EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */
public void afterDetailEval() throws JRScriptletException
...{
super.afterDetailEval();
}
/** *//** End EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't modify or move please! */
public void afterGroupInit(String groupName) throws JRScriptletException
...{
super.afterGroupInit(groupName);
}
/** *//** End EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't modify or move please! */
public void afterPageInit() throws JRScriptletException
...{
super.afterPageInit();
}
/** *//** End EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't modify or move please! */
public void afterReportInit() throws JRScriptletException
...{
}
/** *//** End EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */
public void beforeColumnInit() throws JRScriptletException
...{
}
/** *//** End EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */
public void beforeDetailEval() throws JRScriptletException
...{
}
/** *//** end EVENT_BEFORE_DETAIL_EVAL Please don't touch or move this comment*/
/** *//** End EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't modify or move please! */
public void beforeGroupInit(String groupName) throws JRScriptletException
...{
}
/** *//** End EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't modify or move please! */
public void beforePageInit() throws JRScriptletException
...{
}
/** *//** End EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't modify or move please! */
/** *//** Begin EVENT_BEFORE_REPORT_INIT This line is generated by iReport. Don't modify or move please! */
public void beforeReportInit() throws JRScriptletException
...{
}
/** *//** End EVENT_BEFORE_REPORT_INIT This line is generated by iReport. Don't modify or move please! */
}
1.3 调用Java方法
Java文件已经存在了,方法和类也都定义了,现在来看在iReport怎么使用,拖一个Filed类型的要素到detail部分,如图:
点击右键,选择“Edit Express”,修改内容为:
这样就可以了,其中“$P{REPORT_SCRIPTLET}”可以从下面的Objects and expressions中选取出来,(注意结尾不要加“;”)。
1.4 预览
如果需要预览,则需要把第5.2节中生成的java文件编译成class文件,可以借助Eclipse工程,把java导入到src目录的下(注意,这里为了简单起见,直接拷贝到src下,即default package),然后在对应的目录下找到class文件(如果是Web应用,那么一般都在项目的WEB-INF/classes/目录下),拷贝到和iReport工程的jrxml文件同一目录。
这里要预览还需要修改报表属性,把其Scriptlet class指定为“Use this scriptlet class…”,然后指定class名称为“MyJavaIReportScriptlet”,如图:
点击“OK”后,直接点击 中的第二个就可以看到效果了
2 iReport默认参数和变量的意义
2.1 变量
$V{PAGE_NUMBER}代表当前页数(可以是页码也可以是页数,通过TextField的计算时间的不同值来设置)
$V{PAGE_COUNT}当前页面中记录的数目
$V{GROUPNAME_COUNT}代表当前组的记录数eg:如果组是根据$F{the_city}分组,组名是CityGroup的话,那么此变量为$V{CityGroup_COUNT}
$V{COLUMN_NUMBER}列号码
$V{COLUMN_COUNT}当前列中记录的数目(是当前页里行的序号,在新页上会重置为1)
$V{REPORT_COUNT}当前文档中数据源记录数目
2.2 参数
Parameter name Class type
$P{REPORT_PARAMETERS_MAP} java.util.Map
这是一个内置的参数,这个参数总是指向一个 java.util.Map 对象,该对象保存了用户调用报表装填过程时传递给报表引擎的用户定的参数。
这是一个内置的参数,这个参数总是指向一个 java.util.Map 对象,该对象保存了用户调用报表装填过程时传递给报表引擎的用户定的参数。
$P{REPORT_CONNECTION} java.sql.Connection
这个报表参数指向一个 java.sql.Connection 对象,这个对象被提供给报表引擎用来通过 JDBC 来执行 SQL 报表查询。将 master 报表使用的 JDBC Connection 对象传递给 subreport 是非常有用的,有关这方面信息请查看 subreport 例子
这个报表参数指向一个 java.sql.Connection 对象,这个对象被提供给报表引擎用来通过 JDBC 来执行 SQL 报表查询。将 master 报表使用的 JDBC Connection 对象传递给 subreport 是非常有用的,有关这方面信息请查看 subreport 例子
$P{REPORT_DATA_SOURCE} net.sf.jasperreports.engine.JRDataSource
在报表装填的时候,我们可以或者直接由应用程序中提供,或由报表引擎从所提供的 JDBC Connection 在后台 create 而获得一个数据源。这个内置的参数允许我们在报表表达式中或 scriptlet 中访问报表数据源,而不论我们为什么要这么做。
在报表装填的时候,我们可以或者直接由应用程序中提供,或由报表引擎从所提供的 JDBC Connection 在后台 create 而获得一个数据源。这个内置的参数允许我们在报表表达式中或 scriptlet 中访问报表数据源,而不论我们为什么要这么做。
$P{REPORT_SCRIPTLET} net.sf.jasperreports.engine.JRAbstractScriptlet
即使报表不使用 scriptlet ,这个内置的参数仍将指向一个 net.sf.jasper.engine.JRAbstracStriptlet 实例,该实例实际是一个 net.sf.jasper.engine.JRDefaultScriptlet 对象。
但是当使用 scriptlet 时,报表装填过程所生成的这个指向 scriptlet 类实例的引用允许我们调用其中的某些特殊函数,使用或控制 scriptlet 对象在装填过程中已经准备好的数据。在 scriptlet 例子中你可以看到更详细的使用过程。
即使报表不使用 scriptlet ,这个内置的参数仍将指向一个 net.sf.jasper.engine.JRAbstracStriptlet 实例,该实例实际是一个 net.sf.jasper.engine.JRDefaultScriptlet 对象。
但是当使用 scriptlet 时,报表装填过程所生成的这个指向 scriptlet 类实例的引用允许我们调用其中的某些特殊函数,使用或控制 scriptlet 对象在装填过程中已经准备好的数据。在 scriptlet 例子中你可以看到更详细的使用过程。
- JasperReport初级教程(2.0.3)调用Java代码_图文详解_绝对原创
- JasperReport初级教程(2.0.3)图文详解_绝对原创
- JasperReport初级教程(2.0.3)图文详解_绝对原创 收藏
- JasperReport初级教程(2.0.3)图文详解
- JasperReport初级教程(2.0.3)图文详解
- RedHat Linux安装Oracle10g(图文详解 教程)_绝对原创
- RedHat Linux安装Informix v10.x(图文详解)_绝对原创
- RedHat Linux安装Informix ISA UC6(图文详解)_绝对原创
- RedHat Linux 安装WebLogic9.2 MP2(图文详解 教程下载)_绝对原创
- RedHat Linux 安装WebLogic9.2 MP2(图文详解 教程下载)_绝对原创
- 一步一步学iReport + JasperReports(二) :JasperReport初级教程(2.0.3)图文详解(转)
- PHPCMS教程_标签调用代码详解
- java_父类和接口的引用类型的代码及其详解_原创
- 黑马程序员_实现java快速排序代码详解
- 【图文绝对详细】Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0/Ubuntu14.04
- Java代码导出Jasperreport
- RedHatLinux安装BEA ALBPM5.7 MP2 for WebLogic 9.2(一)_绝对原创
- RedHatLinux安装BEA ALBPM5.7 MP2 for WebLogic 9.2(二)_绝对原创
- 选择方向
- Tomcat里配置CRL
- DIV圆角的制作
- Telnet连接远程计算机
- [转]外企面试常用50问
- JasperReport初级教程(2.0.3)调用Java代码_图文详解_绝对原创
- 成都的IT研发产业和芯片产业等情况:2006年初的数据。(转)
- IT职场:外企面试官最爱提的问题TOP10(转)
- 什么是Web 2.0
- Apache配置完全教程(Linux)
- aspx文件编译成DLL文件的原理
- Oracle系列:程序包
- C语言优先级
- matlab与C# 矩阵转换(Matlab R2008a 正确写法)