您的位置:首页 > 编程语言 > ASP

Java开源报表JasperReport、iReport4.5.1使用详解(四)-JavaBean数据源

2012-05-07 11:09 337 查看
Java开源报表JasperReport、iReport4.5.1使用详解(四)
前几节讲解了iReport的常见的应用,本节讲解一个比较实用的功能,实用JavaBean作为数据源,并结合Struts2来运行。并生成XML、HTML、PDF、XLS文档。
一、用iReport新建一个报表文件
① 前面的步骤省略,不再重复。跟着向导到Query这的时候,选择NewConnection





②选择JavaBeans set datasource




③选择Next,设置一个name 下面的不用管他。
二、设置classpath





添加我们的JavaBean到classpath里面。

三、设置数据源

①选择JavaBean Datasource

输入Class name 然后单击 Read attributes




②选择需要的字段,Add selected field 即可。

在上一步中,有可能出现Read attributes,读取不出来属性的情况,一般请检查下面两点:a).Class 是不是选对了

b).检查classpath

③进行报表设计,排列好字段。这步骤,前面的几节有详细的介绍。

四、和Struts2 整合

我们的iReport的数据源,这里取一个List集合,这个List集合,我们从Struts2中获取。

①导入所需jar包

前面几节有介绍,再次不再赘述,需要注意的是,导入Jar包的时候要避免冲突

②配置Struts2配置文件

<package name="test" namespace="/report" extends="struts-default,jasperreports-default">
<action name="toHtml" class="totalViewAction" method="listTotalView">
<result name="success" type="jasper">
<param name="location">/web/report/groupScore/countView.jasper</param>
<param name="dataSource">listTotaltView</param>
<param name="format">HTML</param>
</result>
<result name="error">
/error.jsp
</result>
<result name="input">
/error.jsp
</result>
</action>
<action name="toPdf" class="totalViewAction" method="listTotalView">
<result name="success" type="jasper">
<param name="location">/web/report/groupScore/countView.jasper</param>
<param name="dataSource">listTotaltView</param>
<param name="format">PDF</param>
</result>
<result name="error">
/error.jsp
</result>
</action>
<action name="toXml" class="totalViewAction" method="listTotalView">
<result name="success" type="jasper">
<param name="location">/web/report/groupScore/countView.jasper</param>
<param name="dataSource">listTotaltView</param>
<param name="format">XML</param>
</result>
<result name="error">
/error.jsp
</result>
</action>
<action name="toXls" class="totalViewAction" method="listTotalView">
<result name="success" type="jasper">
<param name="location">/web/report/groupScore/countView.jasper</param>
<param name="dataSource">listTotaltView</param>
<param name="format">XLS</param>
</result>
<result name="error">
/error.jsp
</result>
</action>
</package>

配置文件详解:

location:指定jasper的地址

dataSource:指定数据源的名称,即Struts2的Action里面的List集合

format:生成的报表的格式

<action name="toPdf" class="totalViewAction" method="listTotalView">
<result name="success" type="jasper">
<param name="location">/web/report/groupScore/countView.jasper</param>
<param name="dataSource">listTotaltView</param>
<param name="format">PDF</param>
</result>
<result name="error">
/error.jsp
</result>
</action>

extends后面需要添加jasperreports-default

<package name="test" namespace="/report" extends="struts-default,jasperreports-default">


本文出自 “幽灵柯南的技术blog” 博客,谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: