四、创建struts.xml文件(放在WEB-INF/classes目录下)
<!DOCTYPE struts SYSTEM "struts-2.0.dtd">
<!-- Configuration for the default package. -->
<struts>
<package name="default" extends="struts-default,jasperreports-default">
<action name="PDF" class="purerock.actions.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="purerock.actions.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="purerock.actions.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="purerock.actions.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="purerock.actions.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>
其实这五个Action配置只是<param name="format">***</param>不一样,也就是报表的形式。
五、web.xml只需提供struts2工程最基本的配置。
<?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>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
六、在WebRoot下新建一名为jasper的文件夹,在这个文件夹下新建一文件名为jasper_template.xml的文件,我们在Action中已经用到它了,这是生成报表的模版,它的内容如下:
<?xml version="1.0"?>
<!DOCTYPE jasperReport
PUBLIC "-//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="jasper_test">
<!-- our fields -->
<field name="name" class="java.lang.String" />
<field name="lastName" class="java.lang.String" />
<title>
<band height="50">
<staticText>
<reportElement x="0" y="0" width="180" height="15" />
<textElement />
<text>
<![CDATA[Webwork JasperReports Sample]]>
</text>
</staticText>
</band>
</title>
<pageHeader>
<band></band>
</pageHeader>
<columnHeader>
<band height="20">
<staticText>
<reportElement x="180" y="0" width="180" height="20" />
<textElement>
<font isUnderline="true" />
</textElement>
<text>
<![CDATA[NAME]]>
</text>
</staticText>
<staticText>
<reportElement x="360" y="0" width="180" height="20" />
<textElement>
<font isUnderline="true" />
</textElement>
<text>
<![CDATA[LASTNAME]]>
</text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20">
<textField>
<reportElement x="180" y="0" width="180" height="15" />
<textElement />
<textFieldExpression>
<![CDATA[$F{name}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="360" y="0" width="180" height="15" />
<textElement />
<textFieldExpression>
<![CDATA[$F{lastName}]]>
</textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band></band>
</columnFooter>
<pageFooter>
<band height="15">
<staticText>
<reportElement x="0" y="0" width="40" height="15" />
<textElement />
<text>
<![CDATA[Page:]]>
</text>
</staticText>
<textField>
<reportElement x="40" y="0" width="100" height="15" />
<textElement />
<textFieldExpression class="java.lang.Integer">
<![CDATA[$V{PAGE_NUMBER}]]>
</textFieldExpression>
</textField>
</band>
</pageFooter>
<summary>
<band></band>
</summary>
</jasperReport>
在Struts2中使用JasperReports生成报表(二)
最新推荐文章于 2015-01-29 13:23:45 发布
