整合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代码如下:
<!-- 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的代码很简单,只在字个字段:city、id、name、street,代码如下所示:
public class 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()
{
return this;
}
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,其代码如下所示:
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;
public class JasperAction extends ActionSupport {
private static final long 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:
< 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:
<! 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 >
这里,当调用JasperAction的execute()方法成功后,返回后,可见有三个参数:location告诉JasperReports数据填充类二进制的.jasper文件的位置,dataSource指明使用什么数据源,format指明报表输出的格式。
六 页面文件
Index.jsp的代码非常的简单,如下:
<! 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 >
七运行效果
1.HTML格式
图2.HTML格式
2.PDF格式
图3.PDF格式
3.XML格式
图4.XML格式
4.Excel格式
图5.Excel格式