根据jasperreport的例子采用applet承载方式展示报表,现在是jasperreports-2.0.2.jar,开发工具我用的是myeclipse6,web框架为struts2.0
1.安装jre(我安装的jre是jre1.6.0_02,下载地址是 http://developers.sun.com/downloads/top.jsp),然后到网站 http://www.jasperforge.org/jaspe ... jr_landingpage.html下载jasperreport的最新版本的zip文件,解压缩后将jasperreports-2.0.2-applet.jar拷贝到jre的lib/ext目录下;
2.将jasperreports-2.0.2-applet.jar 和jasperreports-2.0.2.jar加入到系统中,通常拷贝到web-inf/lib目录下;
3.在webapp下建立一个applets文件夹,把jasperreports-2.0.2/demo/samples/webapp/applets下的EmbeddedViewerApplet.java和JRViewerSimple.java拷贝到系统的src里,文件会被编译成.class,拷贝出来打包成jar并进行数字签名,如下:
a.打包,名字为myjasper.jar
jar -cvf myjasper.jar EmbeddedViewerApplet.class JRViewerSimple.class
b.生成密匙库hehe.store,别名为hehe,有效期为10年
keytool -genkey -keystore hehe.store -alias hehe -validity 3650
c.生成证书hehe.cert
keytool -export -keystore hehe.store -alias hehe -file hehe.cert
d.给myjasper.jar文件进行签名
jarsigner -keystore hehe.store myjasper.jar hehe
把myjasper.jar拷贝到applets目录下;
4.用ireport设计报表(注意在进行报表设计时把jre的lib/ext下的jasperreports-2.0.2-applet.jar文件移走),报表编译成功后,把源文件(.jrxml)拷贝到系统中;
5.在系统的webapp下建立如下jsp文件,起名为reporttemplate.jsp,内容为(基本上拷贝自jasperreports-2.0.2/demo/samples/webapp/viewer.html,做相应修改):
<!--"CONVERTED_APPLET"-->
<!-- HTML CONVERTER -->
<SCRIPT LANGUAGE="JavaScript"><!--
var _info = navigator.userAgent;
var _ns = false;
var _ns6 = false;
var _ie = (_info.indexOf("MSIE") > 0 && _info.indexOf("Win") > 0 && _info.indexOf("Windows 3.1") < 0);
//--></SCRIPT>
<COMMENT>
<SCRIPT LANGUAGE="JavaScript1.1"><!--
var _ns = (navigator.appName.indexOf("Netscape") >= 0 && ((_info.indexOf("Win") > 0 && _info.indexOf("Win16") < 0 && java.lang.System.getProperty("os.version").indexOf("3.5") < 0) || (_info.indexOf("Sun") > 0) || (_info.indexOf("Linux") > 0) || (_info.indexOf("AIX") > 0) || (_info.indexOf("OS/2") > 0)));
var _ns6 = ((_ns == true) && (_info.indexOf("Mozilla/5") >= 0));
//--></SCRIPT>
</COMMENT>
<SCRIPT LANGUAGE="JavaScript"><!--
if (_ie == true) document.writeln('<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" WIDTH = "700" HEIGHT = "500" codebase="<%=request.getContextPath()%>/applets/jre-6u2-windows-i586-p.exe#Version=1,6,0,2"><NOEMBED><XMP>');
else if (_ns == true && _ns6 == false) document.writeln('<EMBED type="application/x-java-applet;version=1.1.2" CODE = "EmbeddedViewerApplet.class" CODEBASE = "<%=request.getContextPath()%>/applets" ARCHIVE = "myjasper.jar" WIDTH = "600" HEIGHT = "400" REPORT_URL = "<%= REPORT_URL%>" scriptable=false pluginspage="http://java.sun.com/products/plugin/1.1.2/plugin-install.html"><NOEMBED><XMP>');
//--></SCRIPT>
<APPLET CODE="EmbeddedViewerApplet.class" CODEBASE="applets"
ARCHIVE="myjasper.jar" WIDTH="700" HEIGHT="500"></XMP>
<PARAM NAME=CODE VALUE="EmbeddedViewerApplet.class">
<PARAM NAME=CODEBASE VALUE="<%=request.getContextPath()%>/applets">
<PARAM NAME=ARCHIVE VALUE="myjasper.jar">
<PARAM NAME="type" VALUE="application/x-java-applet;version=1.6">
<PARAM NAME="scriptable" VALUE="false">
<PARAM NAME="REPORT_URL" VALUE="<%= REPORT_URL%>">
</APPLET>
</NOEMBED>
</EMBED>
</OBJECT>
<!--"END_CONVERTED_APPLET"-->
其中<%= REPORT_URL%> 为action name,可以将参数一同传入,比如:String REPORT_URL = request.getContextPath() + "/namespace/theaction.action?domain1.id="+id.trim();
6.在action中载入报表设计文件并输出报表对象到客户端,一是那个action映射到ReportTemplateAction类
public class ReportTemplateAction extends ActionSupport implements
ServletResponseAware{
......
// 得到报表设计文件xxx.jrxml
private String getReportDesignXML() {
return "com/lan.....xxx.jrxml";
}
protected Collection prepareReportData() {
//数据库相关操作
......
return 检索结果Collection;
}
//主方法
public String list() {
// 得到报表设计文件xxx.jrxml
String reportDesignXML = getReportDesignXML();
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(reportDesignXML);
try {
JasperReport jasperReport = JasperCompileManager.compileReport(in);
//检索数据库所返回的结果集
Collection dataCollection = prepareReportData();
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(
dataCollection);
JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport, parameters, dataSource);
// output report object to client
if (jasperPrint != null) {
response.setContentType("application/octet-stream");
ServletOutputStream ouputStream;
response.reset();
ouputStream = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
oos.writeObject(jasperPrint);
oos.flush();
oos.close();
ouputStream.flush();
ouputStream.close();
}
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
......
}
前后台都编写完毕后,发布运行,在报表界面会出现警告安全对话框,选择始终信任此发行者的内容,点击 “运行”。
通过上面的一个小例子,虽然省略了不少地方,但是基本上把jasperreport涉及到的地方都叙述了,还有系统所用到的jar包太多,也忘记了哪个是jasperreport中用到的,所以如果发现有class not found,自行添加即可
1.安装jre(我安装的jre是jre1.6.0_02,下载地址是 http://developers.sun.com/downloads/top.jsp),然后到网站 http://www.jasperforge.org/jaspe ... jr_landingpage.html下载jasperreport的最新版本的zip文件,解压缩后将jasperreports-2.0.2-applet.jar拷贝到jre的lib/ext目录下;
2.将jasperreports-2.0.2-applet.jar 和jasperreports-2.0.2.jar加入到系统中,通常拷贝到web-inf/lib目录下;
3.在webapp下建立一个applets文件夹,把jasperreports-2.0.2/demo/samples/webapp/applets下的EmbeddedViewerApplet.java和JRViewerSimple.java拷贝到系统的src里,文件会被编译成.class,拷贝出来打包成jar并进行数字签名,如下:
a.打包,名字为myjasper.jar
jar -cvf myjasper.jar EmbeddedViewerApplet.class JRViewerSimple.class
b.生成密匙库hehe.store,别名为hehe,有效期为10年
keytool -genkey -keystore hehe.store -alias hehe -validity 3650
c.生成证书hehe.cert
keytool -export -keystore hehe.store -alias hehe -file hehe.cert
d.给myjasper.jar文件进行签名
jarsigner -keystore hehe.store myjasper.jar hehe
把myjasper.jar拷贝到applets目录下;
4.用ireport设计报表(注意在进行报表设计时把jre的lib/ext下的jasperreports-2.0.2-applet.jar文件移走),报表编译成功后,把源文件(.jrxml)拷贝到系统中;
5.在系统的webapp下建立如下jsp文件,起名为reporttemplate.jsp,内容为(基本上拷贝自jasperreports-2.0.2/demo/samples/webapp/viewer.html,做相应修改):
<!--"CONVERTED_APPLET"-->
<!-- HTML CONVERTER -->
<SCRIPT LANGUAGE="JavaScript"><!--
var _info = navigator.userAgent;
var _ns = false;
var _ns6 = false;
var _ie = (_info.indexOf("MSIE") > 0 && _info.indexOf("Win") > 0 && _info.indexOf("Windows 3.1") < 0);
//--></SCRIPT>
<COMMENT>
<SCRIPT LANGUAGE="JavaScript1.1"><!--
var _ns = (navigator.appName.indexOf("Netscape") >= 0 && ((_info.indexOf("Win") > 0 && _info.indexOf("Win16") < 0 && java.lang.System.getProperty("os.version").indexOf("3.5") < 0) || (_info.indexOf("Sun") > 0) || (_info.indexOf("Linux") > 0) || (_info.indexOf("AIX") > 0) || (_info.indexOf("OS/2") > 0)));
var _ns6 = ((_ns == true) && (_info.indexOf("Mozilla/5") >= 0));
//--></SCRIPT>
</COMMENT>
<SCRIPT LANGUAGE="JavaScript"><!--
if (_ie == true) document.writeln('<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" WIDTH = "700" HEIGHT = "500" codebase="<%=request.getContextPath()%>/applets/jre-6u2-windows-i586-p.exe#Version=1,6,0,2"><NOEMBED><XMP>');
else if (_ns == true && _ns6 == false) document.writeln('<EMBED type="application/x-java-applet;version=1.1.2" CODE = "EmbeddedViewerApplet.class" CODEBASE = "<%=request.getContextPath()%>/applets" ARCHIVE = "myjasper.jar" WIDTH = "600" HEIGHT = "400" REPORT_URL = "<%= REPORT_URL%>" scriptable=false pluginspage="http://java.sun.com/products/plugin/1.1.2/plugin-install.html"><NOEMBED><XMP>');
//--></SCRIPT>
<APPLET CODE="EmbeddedViewerApplet.class" CODEBASE="applets"
ARCHIVE="myjasper.jar" WIDTH="700" HEIGHT="500"></XMP>
<PARAM NAME=CODE VALUE="EmbeddedViewerApplet.class">
<PARAM NAME=CODEBASE VALUE="<%=request.getContextPath()%>/applets">
<PARAM NAME=ARCHIVE VALUE="myjasper.jar">
<PARAM NAME="type" VALUE="application/x-java-applet;version=1.6">
<PARAM NAME="scriptable" VALUE="false">
<PARAM NAME="REPORT_URL" VALUE="<%= REPORT_URL%>">
</APPLET>
</NOEMBED>
</EMBED>
</OBJECT>
<!--"END_CONVERTED_APPLET"-->
其中<%= REPORT_URL%> 为action name,可以将参数一同传入,比如:String REPORT_URL = request.getContextPath() + "/namespace/theaction.action?domain1.id="+id.trim();
6.在action中载入报表设计文件并输出报表对象到客户端,一是那个action映射到ReportTemplateAction类
public class ReportTemplateAction extends ActionSupport implements
ServletResponseAware{
......
// 得到报表设计文件xxx.jrxml
private String getReportDesignXML() {
return "com/lan.....xxx.jrxml";
}
protected Collection prepareReportData() {
//数据库相关操作
......
return 检索结果Collection;
}
//主方法
public String list() {
// 得到报表设计文件xxx.jrxml
String reportDesignXML = getReportDesignXML();
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(reportDesignXML);
try {
JasperReport jasperReport = JasperCompileManager.compileReport(in);
//检索数据库所返回的结果集
Collection dataCollection = prepareReportData();
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(
dataCollection);
JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport, parameters, dataSource);
// output report object to client
if (jasperPrint != null) {
response.setContentType("application/octet-stream");
ServletOutputStream ouputStream;
response.reset();
ouputStream = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
oos.writeObject(jasperPrint);
oos.flush();
oos.close();
ouputStream.flush();
ouputStream.close();
}
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
......
}
前后台都编写完毕后,发布运行,在报表界面会出现警告安全对话框,选择始终信任此发行者的内容,点击 “运行”。
通过上面的一个小例子,虽然省略了不少地方,但是基本上把jasperreport涉及到的地方都叙述了,还有系统所用到的jar包太多,也忘记了哪个是jasperreport中用到的,所以如果发现有class not found,自行添加即可