`
longgangbai
  • 浏览: 7332061 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

整合Struts2+JasperReport Web报表应用示例

 

JasperReports作为一种优秀且开源的报表引擎,不利用太可惜了。同时,借助开源的JasperReports模板设计利器iReports,可以可视化的设计报表模板。基于Web的报表在很多项目中都是需要的。而随着Struts2的流行,就应该研究一下在Struts2下如何利用JasperReports进行报表输出。本示例比较简单,采用的数据源即为JavaBean,而没有采用复杂的数据库,其实原理是一样的。只是传递给JasperReports的参数随着连接方式的变化而略加修改而已。

准备工作

所使用的类库及Jar包如下图所示:


1.使用的Jar

开发环境:MyEclipse5.5+Eclipse3.2+JDK5.0+Tomcat5.5+Struts 2.0.6。当然,本该所提供的源代码下载中没有提供相关的lib,因为文件大太,如果需要,请留下email

源代码下载:Struts2AndJasperReport.rar

设计报表模板

本例中采用JavaBean作为数据源,用于显示People对象的一些基本属性,如所在序号、城市、姓名、地址,并对同一城市的人员进行汇总小结显示。

至于如何在iReports中设计模板文件,请在网上参考其它文档。这里不再详细解读,或有时间俺再另写文章来具体讲解如何在iReports中设计报表模板。WebRoot\jasper\jasper_template.jrxml代码如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!-- Created with iReport - A designer for JasperReports -->
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport
name="jasper_template"
columnCount
="1"
printOrder
="Vertical"
orientation
="Portrait"
pageWidth
="595"
pageHeight
="842"
columnWidth
="535"
columnSpacing
="0"
leftMargin
="30"
rightMargin
="30"
topMargin
="20"
bottomMargin
="20"
whenNoDataType
="NoPages"
isTitleNewPage
="false"
isSummaryNewPage
="false">
<property name="ireport.scriptlethandling" value="0"/>
<property name="ireport.encoding" value="UTF-8"/>
<import value="java.util.*"/>
<import value="net.sf.jasperreports.engine.*"/>
<import value="net.sf.jasperreports.engine.data.*"/>

<parameter name="rptMan" isForPrompting="false" class="java.lang.String">
<defaultValueExpression ><![CDATA["sterning"]]></defaultValueExpression>
</parameter>
<parameter name="rptDate" isForPrompting="false" class="java.lang.String">
<defaultValueExpression ><![CDATA["2008-01-12"]]></defaultValueExpression>
</parameter>

<field name="city" class="java.lang.String">
<fieldDescription><![CDATA[city]]></fieldDescription>
</field>
<field name="street" class="java.lang.String">
<fieldDescription><![CDATA[street]]></fieldDescription>
</field>
<field name="id" class="java.lang.Integer">
<fieldDescription><![CDATA[id]]></fieldDescription>
</field>
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>

<variable name="CityNumber" class="java.lang.Integer" resetType="Group" resetGroup="CityGroup" calculation="Sum">
<initialValueExpression><![CDATA[($V{CityNumber} != null)?(new Integer($V{CityNumber}.intValue() + 1)):(new Integer(1))]]></initialValueExpression>
</variable>
<variable name="AllCity" class="java.lang.Integer" resetType="Report" calculation="Sum">
<initialValueExpression><![CDATA[($V{AllCity} != null)?(new Integer($V{AllCity}.intValue() + 1)):(new Integer(1))]]></initialValueExpression>
</variable>

<group name="CityGroup">
<groupExpression><![CDATA[$F{city}]]></groupExpression>
<groupHeader>
<band height="0" isSplitAllowed="true">
</band>
</groupHeader>
<groupFooter>
<band height="37" isSplitAllowed="true">
<staticText>
<reportElement
x="98"
y
="5"
width
="51"
height
="25"
key
="staticText-8"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font fontName="瀹嬩綋" pdfFontName="STSong-Light" size="14" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[灏忚锛歖]></text>
</staticText>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >
<reportElement
x="149"
y="5"
width="34"
height="25"
key="textField-7"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font fontName="瀹嬩綋" pdfFontName="STSong-Light" size="14" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{CityGroup_COUNT}
]]></textFieldExpression>
</textField>
<line direction="TopDown">
<reportElement
x="-30"
y
="36"
width
="593"
height
="0"
key
="line-5"/>
<graphicElement stretchType="NoStretch"/>
</line>
</band>
</groupFooter>
</group>
<background>
<band height="0" isSplitAllowed="true">
</band>
</background>
<title>
<band height="62" isSplitAllowed="true">
<staticText>
<reportElement
x="20"
y
="0"
width
="484"
height
="57"
forecolor
="#FF0033"
key
="staticText-1"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Center">
<font fontName="瀹嬩綋" pdfFontName="STSong-Light" size="36" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[Struts2+JasperReports绀轰緥]]></text>
</staticText>
<line direction="TopDown">
<reportElement
x="-30"
y
="61"
width
="594"
height
="0"
key
="line-4"/>
<graphicElement stretchType="NoStretch"/>
</line>
</band>
</title>
<pageHeader>
<band height="13" isSplitAllowed="true">
<line direction="TopDown">
<reportElement
x="-29"
y
="12"
width
="592"
height
="0"
key
="line-3"/>
<graphicElement stretchType="NoStretch"/>
</line>
</band>
</pageHeader>
<columnHeader>
<band height="45" isSplitAllowed="true">
<staticText>
<reportElement
x="13"
y
="5"
width
="62"
height
="33"
key
="staticText-2"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font fontName="瀹嬩綋" pdfFontName="STSong-Light" size="24" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[搴忓彿]]></text>
</staticText>
<staticText>
<reportElement
x="98"
y
="5"
width
="62"
height
="33"
key
="staticText-3"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font fontName="瀹嬩綋" pdfFontName="STSong-Light" size="24" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[鍩庡競]]></text>
</staticText>
<staticText>
<reportElement
x="212"
y
="5"
width
="62"
height
="33"
key
="staticText-4"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font fontName="瀹嬩綋" pdfFontName="STSong-Light" size="24" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[濮撳悕]]></text>
</staticText>
<staticText>
<reportElement
x="331"
y
="5"
width
="62"
height
="33"
key
="staticText-5"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font fontName="瀹嬩綋" pdfFontName="STSong-Light" size="24" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
</textElement>
<text><![CDATA[浣忓潃]]></text>
</staticText>
<line direction="TopDown">
<reportElement
x="-30"
y
="43"
width
="596"
height
="0"
key
="line-2"/>
<graphicElement stretchType="NoStretch"/>
</line>
</band>
</columnHeader>
<detail>
<band height="43" isSplitAllowed="true">
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self">
<reportElement
x="13"
y
="7"
width
="62"
height
="30"
key
="textField-1"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font fontName="瀹嬩綋" pdfFontName="STSong-Light" size="14" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression class="java.lang.Integer"><![CDATA[$F{id}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self">
<reportElement
x="98"
y
="7"
width
="105"
height
="29"
key
="textField-2"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font fontName="瀹嬩綋" pdfFontName="STSong-Light" size="14" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{city}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self">
<reportElement
x="212"
y
="8"
width
="100"
height
="28"
key
="textField-3"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font fontName="瀹嬩綋" pdfFontName="STSong-Light" size="14" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self">
<reportElement
x="331"
y
="7"
width
="111"
height
="30"
key
="textField-4"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font fontName="瀹嬩綋" pdfFontName="STSong-Light" size="14" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$F{street}]]></textFieldExpression>
</textField>
<line direction="TopDown">
<reportElement
x="-30"
y
="37"
width
="593"
height
="0"
key
="line-1"/>
<graphicElement stretchType="NoStretch"/>
</line>
</band>
</detail>
<columnFooter>
<band height="37" isSplitAllowed="true">
</band>
</columnFooter>
<pageFooter>
<band height="50" isSplitAllowed="true">
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self">
<reportElement
x="98"
y
="18"
width
="138"
height
="23"
key
="textField-8"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement textAlignment="Right">
<font fontName="瀹嬩綋" pdfFontName="STSong-Light" size="12" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA["椤垫暟 " + String.valueOf($V{PAGE_NUMBER}) + " of"]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Report" hyperlinkType="None" hyperlinkTarget="Self">
<reportElement
x="239"
y
="18"
width
="83"
height
="23"
key
="textField-9"/>
<box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>
<textElement>
<font fontName="瀹嬩綋" pdfFontName="STSong-Light" size="12" isPdfEmbedded ="true" pdfEncoding ="UniGB-UCS2-H"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[" " + String.valueOf($V{PAGE_NUMBER})]]></textFieldExpression>
</textField>
</band>
</pageFooter>
<lastPageFooter>
<band height="50" isSplitAllowed="true">
</band>
</lastPageFooter>
<summary>
<band height="0" isSplitAllowed="true">
</band>
</summary>
</jasperReport>

这里需要说明的是,由于俺们中国的中文比较特殊,由此在PDF中显示的时候,需要一些额外的Jar包字体来支持。因此需要下载iTextAsian.jar包,此包专门用于显示亚洲的字符。

 

设计JavaBean

 

由本例中没有使用数据库,而是使用JavaBean作为JRDataSource,来传递给JasperReports作为数据源,因此只需要设计JavaBean\src\com\sterning\PeopleBean.java的代码很简单,只在字个字段:cityidnamestreet,代码如下所示:

package com.sterning;

publicclass PeopleBean
{
private String city =null;
private Integer id =null;
private String name =null;
private String street =null;

public PeopleBean(
String pcity,
Integer pid,
String pname,
String pstreet
)
{
city
= pcity;
id
= pid;
name
= pname;
street
= pstreet;
}


public PeopleBean getMe()
{
returnthis;
}


public String getCity()
{
return city;
}


public Integer getId()
{
return id;
}


public String getName()
{
return name;
}


public String getStreet()
{
return street;
}

}

 

编译模板

其实表面上说是填充数据,其实就是填充一个List。具体的工具都是交给我们的JasperReports去完成。JasperReports要完成数据的填充工作,要经历将编译(将模板文件.jrxml编译成.jasper文件)、加载(加载.jasper文件)、填充(从数据源中取得数据并填充到.jasper二进制文件中)三大步骤。src\com\sterning\JasperAction.java,其代码如下所示:

package com.sterning;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.sf.jasperreports.engine.JasperCompileManager;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

publicclass JasperAction extends ActionSupport {
privatestaticfinallong serialVersionUID =1L;

private List<PeopleBean> myList;

public String execute() throws Exception {
//添加数据
PeopleBean p1=new PeopleBean("长沙", new Integer(9), "李化", "天洒路");
PeopleBean p2
=new PeopleBean("长沙", new Integer(22), "王小样", "呆小路564");
PeopleBean p3
=new PeopleBean("南昌", new Integer(23), "王奸可", "小顺路");
PeopleBean p4
=new PeopleBean("南昌", new Integer(32), "李洒", "顺濉路");
PeopleBean p5
=new PeopleBean("武汉", new Integer(39), "张中尖", "天洒路");
PeopleBean p6
=new PeopleBean("武汉", new Integer(35), "陈主宁", "天河路564");
myList
=new ArrayList<PeopleBean>();
myList.add(p1);
myList.add(p2);
myList.add(p3);
myList.add(p4);
myList.add(p5);
myList.add(p6);

try{
String reportSource;
reportSource
= ServletActionContext.getServletContext()
.getRealPath(
"/jasper/jasper_template.jrxml");
File parent
=new File(reportSource).getParentFile();
//将.jrxml模板文件编译成为.jasper文件,当然,其文件名可以指定,如果没指定,则与.jrxml文件名一样.只是后缀不同而已
JasperCompileManager.compileReportToFile(reportSource, new File(
parent,
"compiled_jasper_template.jasper")
.getAbsolutePath());
}
catch (Exception e) {
e.printStackTrace();
return ERROR;
}

return SUCCESS;
}


public List getMyList() {
return myList;
}

}

 

配置Struts2文件

前面这些工作都是为JasperReports而作的。接下来就是将JasperReports集成到Struts2中。

1.Web.xml

首先是Web.xml。这个文件配置比较简单,也比较常规,没什么好说,代码如下WebRoot\WEB-INF\web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>
struts-default.xml,struts-plugin.xml,struts.xml
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- The Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

 

2.struts.xml

按理说struts.xml配置文件应该更加简单,但这里由于集成了JasperReports,所以看上去要复杂一点。先来看代码:src\ struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"
>
<struts>
<package name="default"
extends
="struts-default,jasperreports-default">
<action name="PDF" class="com.sterning.JasperAction">
<result name="success" type="jasper">
<param name="location">
/jasper/compiled_jasper_template.jasper
</param>
<param name="dataSource">myList</param>
<param name="format">PDF</param>
</result>
</action>
<action name="HTML" class="com.sterning.JasperAction">
<result name="success" type="jasper">
<param name="location">
/jasper/compiled_jasper_template.jasper
</param>
<param name="dataSource">myList</param>
<param name="format">HTML</param>
</result>
</action>
<action name="XML" class="com.sterning.JasperAction">
<result name="success" type="jasper">
<param name="location">
/jasper/compiled_jasper_template.jasper
</param>
<param name="dataSource">myList</param>
<param name="format">XML</param>
</result>
</action>
<action name="CSV" class="com.sterning.JasperAction">
<result name="success" type="jasper">
<param name="location">
/jasper/compiled_jasper_template.jasper
</param>
<param name="dataSource">myList</param>
<param name="format">CSV</param>
</result>
</action>
<action name="XLS" class="com.sterning.JasperAction">
<result name="success" type="jasper">
<param name="location">
/jasper/compiled_jasper_template.jasper
</param>
<param name="dataSource">myList</param>
<param name="format">XLS</param>
</result>
</action>
</package>
</struts>

这里,当调用JasperActionexecute()方法成功后,返回后,可见有三个参数:location告诉JasperReports数据填充类二进制的.jasper文件的位置,dataSource指明使用什么数据源,format指明报表输出的格式。

 

页面文件

Index.jsp的代码非常的简单,如下:

<%@ page language="java" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Struts2+ JasperReports 使用示例</title>
</head>
<body>
<a href="HTML.action">HTML</a>
<br>
<a href="PDF.action">PDF</a>
<br>
<a href="XML.action">XML</a>
<br>
<a href="CSV.action">CSV</a>
<br>
<a href="XLS.action">XLS</a>
</body>
</html>

 

运行效果

 

分享到:
评论

相关推荐

    Struts2+JasperReport报表应用

    Struts2和JasperReport的整合是Web应用程序中创建动态报表的一种常见方法。JasperReport是一个功能强大的开源报表引擎,允许开发人员通过iReports这样的设计工具以可视化的形式创建复杂的报表模板。Struts2,作为一...

    struts2+Ireport+Jasperreport实现报表导出

    Struts2、iReport与JasperReport是Java开发中用于构建高效、动态Web应用程序和报表生成的工具。在这个项目中,这些技术结合在一起,提供了一种强大的报表导出解决方案。以下是对这些关键技术及其在项目中的应用的...

    struts2整合jasperreport

    在本文中,我们将深入探讨如何整合Struts2框架与JasperReport3来创建动态报表,并利用iReport工具进行报表设计。Struts2是一个流行的Java Web应用程序框架,它提供了MVC(模型-视图-控制器)架构,而JasperReport则...

    Struts2与jasperReport简单示例

    4. **整合Struts2**:创建一个Struts2 Action,这个Action负责生成报表。在Action中调用上述步骤生成报表实例,然后将其设置为Action的一个属性,以便传递到视图。 5. **配置结果类型**:在struts.xml中,为这个...

    Struts2 + JasperReports4 + DB2 Demo

    这个Demo项目对于初学者和有经验的开发者来说都是一个宝贵的资源,它提供了实际操作的机会,帮助理解如何在实际项目中整合Struts2、JasperReports和DB2,以实现数据驱动的报表生成。通过深入研究这个示例,你可以...

    jasperreport+struts2包

    总的来说,jasperreport与Struts2的整合为Web应用带来了强大的报表生成能力,但同时也要注意性能优化和安全防护。通过熟练掌握这两者的结合,开发者可以构建出功能完善的业务系统,提供直观的数据展示。

    Struts2AndJasperReport实例

    Struts2 是一个基于 Model-View-Controller(MVC)架构模式的 web 应用框架,而 JasperReport 是一个用于创建、设计和展示报表的库,它支持多种数据源和输出格式。 首先,让我们深入了解一下 Struts2。Struts2 是 ...

    Ireport4集成Struts2

    2. **配置数据源**:在Struts2应用中配置数据库连接,通常是通过Spring等依赖注入框架管理DataSource。 3. **设计报表**:使用iReport设计报表模板,保存为.jrxml文件。 4. **生成报表**:在Struts2 Action中,使用...

    JasperReport+SSH

    总结起来,"JasperReport+SSH"项目是一个利用Java技术栈,包括JasperReport进行报表生成,Struts处理前端请求,Spring管理依赖和事务,Hibernate进行数据库操作,以及ExtJS 4.1构建用户界面的综合应用。开发者可以...

Global site tag (gtag.js) - Google Analytics