在Struts2中使用JasperReports生成报表

在应用中,我们普遍是先通过iReport等工具生成编译后的*.jasper,然后交Struts2,Struts2的Action负责提供数据源及组装.

 

本例为了说明更清楚,我们用iReport只生成jasper_template.xml,即没经过编译的报表设计文件,那么Struts2有多了一项任务就是编译报表。好了我们进正题。

 

一、在struts2中使用jasperreports生成报表除了常规struts2工程需要的Jar文件之外还需要两个Jar文件
1、struts2-jasperreports-plugin-2.0.11.jar(在struts2发布包的lib目录下可以找到)(必须)
2、jasperreports-2.0.2.jar(在jasperreports的发布包中,我用的2.0.2是最新版本)(必须)

3、如果要在报表中输出中文还需要俩Jar  (iTextAsian.jar,itext-1.3.1.jar)

4、此代码测试没错,如果在运行中出错,只有一个可能,检查jar(常规struts2工程需要的Jar文件不全

二、index.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JasperReport</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>
三、其实上面几个链接要引用的是同一个Action,下面让我们看看这个Action,在引入Action之前先让我们看看Action要使用的JavaBean Person
package purerock.actions;
public class Person {
  private Long id;
  private String name;
  private String lastName;
  public Person() {
    super();
  }
  public Person(String name, String lastName) {
    super();
    this.name = name;
    this.lastName = lastName;
  }
  public Person(Long id, String name, String lastName) {
    super();
    this.id = id;
    this.name = name;
    this.lastName = lastName;
  }

  public Long getId() {
    return id;
  }
  public void setId(Long id) {
    this.id = id;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}
这是一个极其普通的JavaBean,相信你很熟悉它了。现在让我们看看Action
package purerock.actions;
//imports
import java.util.ArrayList;
import java.util.List;
import java.io.File;
import net.sf.jasperreports.engine.JasperCompileManager;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

public class JasperAction extends ActionSupport {
  private static final long serialVersionUID = 1L;
  private List<Person> myList;
  public String execute() throws Exception {
    //create some imaginary persons
    Person p1 = new Person(new Long(1), "Patrick", "Lightbuddie");
    Person p2 = new Person(new Long(2), "Jason", "Carrora");
    Person p3 = new Person(new Long(3), "Alexandru", "Papesco");
    Person p4 = new Person(new Long(4), "Jay", "Boss");
    myList = new ArrayList<Person>();
    myList.add(p1);
    myList.add(p2);
    myList.add(p3);
    myList.add(p4);
    /*
     * Here we compile our xml jasper template to a jasper file.
     * Note: this isn't exactly considered 'good practice'.
     * You should either use precompiled jasper files (.jasper) or provide some kind of check
     * to make sure you're not compiling the file on every request.
     * If you don't have to compile the report, you just setup your data source (eg. a List)
     */
    try {
      String reportSource;
      reportSource=ServletActionContext.getServletContext().getRealPath("/jasper/jasper_template.xml");
      File parent = new File(reportSource).getParentFile();
      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;
  }
}
说明:在这里为了演示的方便我们手动创建了一个存有几个Person对象的List,在实际应用中应该是从数据库中加载数据。
四、创建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>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值