日期:2014-05-17  浏览次数:20968 次

在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();