posts - 208, comments - 3948, trackbacks - 0, articles - 3
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

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

Posted on 2008-01-02 22:20 诗特林 阅读(3054) 评论(46)  编辑  收藏 所属分类: BI
 

整合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"