当前位置: 代码迷 >> ASP >> 在Struts2中应用JasperReports生成报表
  详细解决方案

在Struts2中应用JasperReports生成报表

热度:406   发布时间:2012-10-27 10:42:26.0
在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>

?

?

1 楼 dxwang 2008-03-03  
不知道可否在struts2中做过多表的嵌套取数据?
2 楼 mht19840918 2008-03-04  
我目前还未遇到,呵呵,欢迎共享你的经验
3 楼 zw7534313 2008-04-22  
     我的咋运行不了,上面的都有了,就是不知道哪有问题?那个jasper文件咋没有呢?
  zw7534313@163.com
4 楼 ggh1232007 2008-08-02  
你好!我的程序怎么在编译 .jrxml文件那句报异常 :


java.lang.reflect.InvocationTargetException


不知道什么原因!谢谢!
5 楼 mht19840918 2008-08-11  
你确信jar包全了吗?
6 楼 kevin_gzhz 2008-09-05  
有没有在struts2下开发子报表的例子,子表报的数据源为集合对象的javabean,谢谢
7 楼 jy02411368 2008-10-07  
没有JASPER文件是因为 你还没有进行编译 需要编译 最好用工具 编译也就是指 在IREPORT工具里 导出一次 例如PDF然后 你的文件夹里就有有 XXX.JASPER这个文件了
8 楼 mht19840918 2008-10-07  
不好意思,老久没来看看了。其实以下这段就是编译
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());

当然我是在action中进行的,当然你通过IREPORT工具是我们推荐的,这个例子中仅仅是一个包括编译在内所走的一整套流程,我懒,都是从官方例子搬来的,对不起各位
9 楼 BlackSwallow 2008-10-16  
每一个格式就要写一个action,太麻烦了?format等参数的值不能传进去吗?
10 楼 mht19840918 2008-10-16  
BlackSwallow 写道

每一个格式就要写一个action,太麻烦了?format等参数的值不能传进去吗?

其实总共就一个action,但是被重复配置了下。
11 楼 lansky07 2009-02-07  
mht19840918 写道

BlackSwallow 写道
每一个格式就要写一个action,太麻烦了?format等参数的值不能传进去吗? 其实总共就一个action,但是被重复配置了下。

可以通配符的方式来实现,只配置一次
<action name="action_*" 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">{1}</param>
        </result>
      </action>

这样写一个配置就可以了,写调用时只要action_HTML或者action_PDF等就可以了
12 楼 wslfh2008 2009-03-13  
13 楼 yangpeihai 2009-12-04  
我怎么一运行就报:
org.xml.sax.SAXNotRecognizedException: http://apache.org/xml/features/validation/dynamic
at com.caucho.xml.parsers.XmlSAXParserFactory.setFeature(XmlSAXParserFactory.java:94)
at org.apache.commons.digester.parser.XercesParser.configureXerces(XercesParser.java:185)
at org.apache.commons.digester.parser.XercesParser.newSAXParser(XercesParser.java:138)
at org.apache.commons.digester.ParserFeatureSetterFactory.newSAXParser(ParserFeatureSetterFactory.java:71)
at org.apache.commons.digester.Digester.getParser(Digester.java:692)
at org.apache.commons.digester.Digester.getXMLReader(Digester.java:899)
at org.apache.commons.digester.Digester.parse(Digester.java:1647)
at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:239)
at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:226)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:214)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:168)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:152)
at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:108)
at purerock.actions.JasperAction.execute(JasperAction.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
14 楼 mht19840918 2009-12-08  
yangpeihai 写道
我怎么一运行就报:
org.xml.sax.SAXNotRecognizedException: http://apache.org/xml/features/validation/dynamic
at com.caucho.xml.parsers.XmlSAXParserFactory.setFeature(XmlSAXParserFactory.java:94)
at org.apache.commons.digester.parser.XercesParser.configureXerces(XercesParser.java:185)
at org.apache.commons.digester.parser.XercesParser.newSAXParser(XercesParser.java:138)
at org.apache.commons.digester.ParserFeatureSetterFactory.newSAXParser(ParserFeatureSetterFactory.java:71)
at org.apache.commons.digester.Digester.getParser(Digester.java:692)
at org.apache.commons.digester.Digester.getXMLReader(Digester.java:899)
at org.apache.commons.digester.Digester.parse(Digester.java:1647)
at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:239)
at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:226)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:214)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:168)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:152)
at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:108)
at purerock.actions.JasperAction.execute(JasperAction.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)


java.lang.NoClassDefFoundError: org/xml/sax/SAXNotRecognizedException  
你需要保证xerces.jar文件在CLASSPATH中位于其他XML类,如JAXP或Crimson之前,这些类文件,包括以前老版本的xerces,可能不支持SAX2.0或DOM Level 2。于是导致了上面的错误。
15 楼 xiaobojava 2009-12-10  
请问博主,<result name="success" type="jasper"></result>之间的param还有哪些呢?
如何直接调用打印机?
16 楼 oneteams 2010-06-15  
本人是菜鸟,刚刚开始研究jasperreport,看了楼主的文章,想看看楼主自定义的那个result--jasper代码
17 楼 veteran123 2010-07-20  
我用struts2 向 编译好的jasper文件里面 传递List<bean>
其中Bean里面有若干条String 的属性。

是生成了pdf 可是只能找出List中的第一个bean,而且是被放在column header 里面才能显示
如果放在了detail中,根本不显示。
我很郁闷,是什么原因啊?
18 楼 kangguolong 2011-11-09  
format=PDF没问题,但是为html时就出现图片找不到的问题。怎么办,都纠结好久了。
19 楼 w156445045 2012-05-07  
kangguolong 写道
format=PDF没问题,但是为html时就出现图片找不到的问题。怎么办,都纠结好久了。

这个需要在加入一段代码~
20 楼 hutengfei0701 2012-09-26  
如果生成的报表HTML上还需要加入几个按钮之类的,怎么做? strust2配置生成的报表,页面好像是自动生成的。
  相关解决方案