在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,在实际应用中应该是从数据库中加载数据。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值