在应用中,我们普遍是先通过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>
?
?
zw7534313@163.com
java.lang.reflect.InvocationTargetException
不知道什么原因!谢谢!
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工具是我们推荐的,这个例子中仅仅是一个包括编译在内所走的一整套流程,我懒,都是从官方例子搬来的,对不起各位
每一个格式就要写一个action,太麻烦了?format等参数的值不能传进去吗?
其实总共就一个action,但是被重复配置了下。
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等就可以了
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)
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。于是导致了上面的错误。
如何直接调用打印机?
其中Bean里面有若干条String 的属性。
是生成了pdf 可是只能找出List中的第一个bean,而且是被放在column header 里面才能显示
如果放在了detail中,根本不显示。
我很郁闷,是什么原因啊?
这个需要在加入一段代码~