struts2整合jasper模版生成PDF导出下载<一>

被这个问题困扰了好几天,今天终于解决。现在我将呈现如何用Struts2整合jasperreport生成pdf文件,并且提供查看和下载功能:

一,制作jasper模版
首先我才用的是ireport4.1.3制作模版,模版代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report3" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="0">
	<property name="ireport.zoom" value="1.0"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="263"/>
	<style name="table">
		<box>
			<pen lineWidth="1.0" lineColor="#000000"/>
		</box>
	</style>
	<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
		<box>
			<pen lineWidth="0.5" lineColor="#000000"/>
		</box>
	</style>
	<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
		<box>
			<pen lineWidth="0.5" lineColor="#000000"/>
		</box>
	</style>
	<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
		<box>
			<pen lineWidth="0.5" lineColor="#000000"/>
		</box>
	</style>
	<subDataset name="Table Dataset 1"/>
	<parameter name="chineseName" class="java.lang.String"/>
	<parameter name="englishName" class="java.lang.String"/>
	<parameter name="memberNo" class="java.lang.String"/>
	<parameter name="memberTier" class="java.lang.String"/>
	<parameter name="IDCard" class="java.lang.String"/>
	<parameter name="passport" class="java.lang.String"/>
	<parameter name="others" class="java.lang.String"/>
	<parameter name="applyDate" class="java.lang.String"/>
	<parameter name="memberTierEN" class="java.lang.String"/>
	<field name="flightDate" class="java.sql.Timestamp"/>
	<field name="flightNumber" class="java.lang.String"/>
	<field name="departureAirport" class="java.lang.String"/>
	<field name="arriveAirport" class="java.lang.String"/>
	<background>
		<band height="807" splitType="Stretch">
			<image>
				<reportElement x="0" y="0" width="555" height="807"/>
				<imageExpression><![CDATA["http://10.103.99.6/skypearl/jasreportTemplete/visabackgroundone.jpg"]]></imageExpression>
			</image>
		</band>
	</background>
	<title>
		<band height="491" splitType="Stretch">
			<staticText>
				<reportElement x="0" y="110" width="555" height="27"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="15" isBold="true" isItalic="false" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
					<paragraph lineSpacing="Single"/>
				</textElement>
				<text><![CDATA[南航明珠俱乐部金、银卡认证信
]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="194" width="555" height="27"/>
				<textElement verticalAlignment="Middle">
					<font size="10" isBold="true" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[Personal Information in the China Southern Airlines Sky Pearl Club ]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="221" width="54" height="20"/>
				<textElement verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[中文姓名]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="241" width="54" height="20"/>
				<textElement verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[英文姓名]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="261" width="54" height="24"/>
				<textElement verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[会员号码]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="285" width="54" height="20"/>
				<textElement verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[会员级别]]></text>
			</staticText>
			<textField isBlankWhenNull="true">
				<reportElement x="159" y="221" width="396" height="20"/>
				<textElement textAlignment="Left" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{chineseName}]]></textFieldExpression>
			</textField>
			<textField isBlankWhenNull="true">
				<reportElement x="201" y="241" width="354" height="22"/>
				<textElement textAlignment="Left" verticalAlignment="Middle">
					<font size="12" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{englishName}]]></textFieldExpression>
			</textField>
			<textField isBlankWhenNull="true">
				<reportElement x="180" y="261" width="375" height="24"/>
				<textElement textAlignment="Left" verticalAlignment="Middle">
					<font size="12" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{memberNo}]]></textFieldExpression>
			</textField>
			<textField isBlankWhenNull="true">
				<reportElement x="159" y="285" width="106" height="20"/>
				<textElement textAlignment="Left" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{memberTier}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement x="0" y="305" width="77" height="22"/>
				<textElement verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[会员旅行证件]]></text>
			</staticText>
			<staticText>
				<reportElement style="table_TH" stretchType="RelativeToTallestObject" x="159" y="327" width="293" height="22"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[]]></text>
			</staticText>
			<staticText>
				<reportElement style="table_TH" stretchType="RelativeToTallestObject" x="0" y="349" width="159" height="20"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[]]></text>
			</staticText>
			<staticText>
				<reportElement style="table_TH" stretchType="RelativeToTallestObject" x="0" y="369" width="159" height="20"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[]]></text>
			</staticText>
			<staticText>
				<reportElement style="table_TH" stretchType="RelativeToTallestObject" x="0" y="389" width="164" height="20"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[]]></text>
			</staticText>
			<textField isBlankWhenNull="true">
				<reportElement style="table_TD" stretchType="RelativeToTallestObject" x="159" y="349" width="293" height="20"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="10"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{IDCard}]]></textFieldExpression>
			</textField>
			<textField isBlankWhenNull="true">
				<reportElement style="table_TD" stretchType="RelativeToTallestObject" x="159" y="369" width="293" height="20"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="10" isPdfEmbedded="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{passport}]]></textFieldExpression>
			</textField>
			<textField isBlankWhenNull="true">
				<reportElement style="table_TD" stretchType="RelativeToTallestObject" x="159" y="389" width="293" height="20"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="10" isPdfEmbedded="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{others}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement x="0" y="434" width="575" height="18"/>
				<textElement verticalAlignment="Middle">
					<font size="10" isBold="true" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[Flight Information within the previous 24 month period in the China Southern Airlines Sky Pearl Club]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="137" width="555" height="34"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" isBold="true" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[China Southern Airlines Sky Pearl Club Gold and Silver Card Letter of Certification]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="171" width="555" height="23"/>
				<textElement verticalAlignment="Middle">
					<font size="10" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[在南航明珠俱乐部登记的个人资料]]></text>
			</staticText>
			<staticText>
				<reportElement x="54" y="221" width="110" height="20"/>
				<textElement verticalAlignment="Middle">
					<font size="12" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[ Name in Chinese: ]]></text>
			</staticText>
			<staticText>
				<reportElement x="51" y="241" width="161" height="22"/>
				<textElement verticalAlignment="Middle">
					<font size="12" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[ Name in English or Pinyin: ]]></text>
			</staticText>
			<staticText>
				<reportElement x="54" y="263" width="136" height="22"/>
				<textElement verticalAlignment="Middle">
					<font size="12" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[ Membership Number:]]></text>
			</staticText>
			<staticText>
				<reportElement x="54" y="285" width="110" height="20"/>
				<textElement verticalAlignment="Middle">
					<font size="12" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[ Membership Tier: ]]></text>
			</staticText>
			<staticText>
				<reportElement x="77" y="305" width="246" height="22"/>
				<textElement verticalAlignment="Middle">
					<font size="12" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[ Member’s Travel Document:]]></text>
			</staticText>
			<staticText>
				<reportElement style="table_TH" x="0" y="327" width="159" height="22"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="327" width="54" height="22"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[旅行证件]]></text>
			</staticText>
			<staticText>
				<reportElement x="54" y="327" width="105" height="22"/>
				<textElement textAlignment="Left" verticalAlignment="Middle">
					<font size="12"/>
				</textElement>
				<text><![CDATA[ Travel Document]]></text>
			</staticText>
			<staticText>
				<reportElement stretchType="RelativeToTallestObject" x="0" y="349" width="54" height="20"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[身份证]]></text>
			</staticText>
			<staticText>
				<reportElement stretchType="RelativeToTallestObject" x="51" y="349" width="108" height="20"/>
				<textElement textAlignment="Left" verticalAlignment="Middle">
					<font size="12"/>
				</textElement>
				<text><![CDATA[ID Card]]></text>
			</staticText>
			<staticText>
				<reportElement stretchType="RelativeToTallestObject" x="0" y="369" width="54" height="20"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[护照]]></text>
			</staticText>
			<staticText>
				<reportElement stretchType="RelativeToTallestObject" x="54" y="389" width="105" height="20"/>
				<textElement textAlignment="Left" verticalAlignment="Middle">
					<font size="12"/>
				</textElement>
				<text><![CDATA[Passport]]></text>
			</staticText>
			<staticText>
				<reportElement stretchType="RelativeToTallestObject" x="0" y="389" width="54" height="20"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[其他]]></text>
			</staticText>
			<staticText>
				<reportElement stretchType="RelativeToTallestObject" x="54" y="369" width="105" height="20"/>
				<textElement textAlignment="Left" verticalAlignment="Middle">
					<font size="12"/>
				</textElement>
				<text><![CDATA[Others]]></text>
			</staticText>
			<staticText>
				<reportElement x="159" y="327" width="139" height="22"/>
				<textElement textAlignment="Right" verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[号码 ]]></text>
			</staticText>
			<staticText>
				<reportElement x="298" y="327" width="154" height="22"/>
				<textElement textAlignment="Left" verticalAlignment="Middle">
					<font size="12"/>
				</textElement>
				<text><![CDATA[Number]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="419" width="575" height="18"/>
				<textElement verticalAlignment="Middle">
					<font size="10" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[在南航明珠俱乐部登记的24个月内乘机记录]]></text>
			</staticText>
			<textField isBlankWhenNull="true">
				<reportElement x="201" y="285" width="354" height="20"/>
				<textElement verticalAlignment="Middle">
					<font size="12"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{memberTierEN}]]></textFieldExpression>
			</textField>
			<image>
				<reportElement x="0" y="0" width="555" height="110"/>
				<imageExpression><![CDATA["http://10.103.99.6/skypearl/jasreportTemplete/yemei.jpg"]]></imageExpression>
			</image>
			<staticText>
				<reportElement style="table_TH" mode="Opaque" x="190" y="461" width="131" height="30"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[]]></text>
			</staticText>
			<staticText>
				<reportElement style="table_TH" mode="Opaque" x="0" y="461" width="95" height="30"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[]]></text>
			</staticText>
			<staticText>
				<reportElement style="table_TH" mode="Opaque" x="95" y="461" width="95" height="30"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[]]></text>
			</staticText>
			<staticText>
				<reportElement style="table_TH" mode="Opaque" x="321" y="461" width="131" height="30"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="12" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="476" width="95" height="15"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="10"/>
				</textElement>
				<text><![CDATA[Date of Flight]]></text>
			</staticText>
			<staticText>
				<reportElement x="95" y="476" width="95" height="15"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="10"/>
				</textElement>
				<text><![CDATA[Flight Number]]></text>
			</staticText>
			<staticText>
				<reportElement x="321" y="461" width="131" height="15"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="10" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[目的地]]></text>
			</staticText>
			<staticText>
				<reportElement x="321" y="476" width="131" height="15"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="10"/>
				</textElement>
				<text><![CDATA[To]]></text>
			</staticText>
			<staticText>
				<reportElement x="190" y="476" width="131" height="15"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="10"/>
				</textElement>
				<text><![CDATA[From]]></text>
			</staticText>
			<staticText>
				<reportElement x="190" y="461" width="131" height="15"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="10" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[始发地]]></text>
			</staticText>
			<staticText>
				<reportElement x="95" y="461" width="95" height="15"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="10" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[航班号]]></text>
			</staticText>
			<staticText>
				<reportElement x="0" y="461" width="95" height="15"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="10" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[航班日期
]]></text>
			</staticText>
		</band>
	</title>
	<pageHeader>
		<band splitType="Stretch"/>
	</pageHeader>
	<columnHeader>
		<band/>
	</columnHeader>
	<detail>
		<band height="19" splitType="Stretch">
			<textField isStretchWithOverflow="true" isBlankWhenNull="true">
				<reportElement style="table_TD" stretchType="RelativeToTallestObject" x="95" y="0" width="95" height="19"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="10" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{flightNumber}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="true">
				<reportElement style="table_TD" stretchType="RelativeToTallestObject" x="190" y="0" width="131" height="19"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="10" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{departureAirport}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" pattern="yyyy/MM/dd" isBlankWhenNull="true">
				<reportElement style="table_TD" stretchType="RelativeToTallestObject" x="0" y="0" width="95" height="19"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="10" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{flightDate}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true" isBlankWhenNull="true">
				<reportElement style="table_TD" stretchType="RelativeToTallestObject" x="321" y="0" width="131" height="19"/>
				<textElement textAlignment="Center" verticalAlignment="Middle">
					<font size="10" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{arriveAirport}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
	<columnFooter>
		<band/>
	</columnFooter>
	<pageFooter>
		<band height="25" splitType="Stretch"/>
	</pageFooter>
	<lastPageFooter>
		<band height="187">
			<staticText>
				<reportElement x="0" y="0" width="54" height="22"/>
				<textElement verticalAlignment="Middle">
					<font size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
				</textElement>
				<text><![CDATA[申请日期]]></text>
			</staticText>
			<textField isBlankWhenNull="true">
				<reportElement x="164" y="0" width="389" height="22"/>
				<textElement verticalAlignment="Middle">
					<font size="12"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{applyDate}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement x="54" y="0" width="122" height="22"/>
				<textElement verticalAlignment="Middle">
					<font size="12" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
				</textElement>
				<text><![CDATA[Date of Application:]]></text>
			</staticText>
			<image>
				<reportElement x="0" y="22" width="553" height="165"/>
				<imageExpression><![CDATA["http://10.103.99.6/skypearl/jasreportTemplete/yejiaoone.jpg"]]></imageExpression>
			</image>
		</band>
	</lastPageFooter>
	<summary>
		<band height="42" splitType="Stretch"/>
	</summary>
</jasperReport>

你可能感兴趣的:(struts2,jasperReport)