JasperReport+Servlet Web应用之:打印预览
打印预览与打印的准备工作是一样的,第一,二,三,四,五步请参照:JasperReport+Servlet Web应用之:打印?
不同之处在于:我们同样要写一个预览的applet如下:
import java.io.PrintWriter;import java.io.StringWriter;import java.net.URL;import javax.swing.JOptionPane;import net.sf.jasperreports.engine.JasperPrint;import net.sf.jasperreports.engine.JasperPrintManager;import net.sf.jasperreports.engine.util.JRLoader;public class JRViewApplet extends javax.swing.JApplet {private URL url = null;private JasperPrint jasperPrint = null;public JRViewApplet() {}public void init() {String strUrl = getParameter("REPORT_URL");if (strUrl != null) {try {url = new URL(getCodeBase(), strUrl);} catch (Exception e) {StringWriter swriter = new StringWriter();PrintWriter pwriter = new PrintWriter(swriter);e.printStackTrace(pwriter);JOptionPane.showMessageDialog(this, swriter.toString());}} else {JOptionPane.showMessageDialog(this, "Source URL not specified");}}@Overridepublic void start() {// initComponents();if (url != null) {try {if (jasperPrint == null) {jasperPrint = (JasperPrint) JRLoader.loadObject(url);}if (jasperPrint != null) {ViewerFrame viewerFrame = new ViewerFrame(this.getAppletContext(), jasperPrint);viewerFrame.show();} else {JOptionPane.showMessageDialog(this, "空报表!");}} catch (Exception e) {StringWriter swriter = new StringWriter();PrintWriter pwriter = new PrintWriter(swriter);e.printStackTrace(pwriter);JOptionPane.showMessageDialog(this, swriter.toString());}} else {JOptionPane.showMessageDialog(this, "Source URL not specified");}}private void initComponents() {pnlMain = new javax.swing.JPanel();btnPrint = new javax.swing.JButton();btnView = new javax.swing.JButton();// btnPrint.setText("打印报表");// btnPrint.addActionListener(new java.awt.event.ActionListener() {// public void actionPerformed(java.awt.event.ActionEvent evt) {// btnPrintActionPerformed(evt);// }// });// pnlMain.add(btnPrint);btnView.setText("预览报表");btnView.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {btnViewActionPerformed(evt);}});pnlMain.add(btnView);getContentPane().add(pnlMain, java.awt.BorderLayout.WEST);}/** * 预览报表 * * @param evt */protected void btnViewActionPerformed(java.awt.event.ActionEvent evt) {if (url != null) {try {if (jasperPrint == null) {jasperPrint = (JasperPrint) JRLoader.loadObject(url);}if (jasperPrint != null) {ViewerFrame viewerFrame = new ViewerFrame(this.getAppletContext(), jasperPrint);viewerFrame.show();} else {JOptionPane.showMessageDialog(this, "空报表!");}} catch (Exception e) {StringWriter swriter = new StringWriter();PrintWriter pwriter = new PrintWriter(swriter);e.printStackTrace(pwriter);JOptionPane.showMessageDialog(this, swriter.toString());}} else {JOptionPane.showMessageDialog(this, "Source URL not specified");}}/** * 打印报表 * * @param evt */protected void btnPrintActionPerformed(java.awt.event.ActionEvent evt) {if (url != null) {if (jasperPrint == null) {try {jasperPrint = (JasperPrint) JRLoader.loadObject(url);} catch (Exception e) {StringWriter swriter = new StringWriter();PrintWriter pwriter = new PrintWriter(swriter);e.printStackTrace(pwriter);JOptionPane.showMessageDialog(this, swriter.toString());}}if (jasperPrint != null) {final JasperPrint print = jasperPrint;Thread thread = new Thread(new Runnable() {public void run() {try {JasperPrintManager.printReport(print, true);} catch (Exception e) {StringWriter swriter = new StringWriter();PrintWriter pwriter = new PrintWriter(swriter);e.printStackTrace(pwriter);JOptionPane.showMessageDialog(null, swriter.toString());}}});thread.start();} else {JOptionPane.showMessageDialog(this, "空报表!");}} else {JOptionPane.showMessageDialog(this, "Source URL not specified");}}private javax.swing.JPanel pnlMain;private javax.swing.JButton btnView;private javax.swing.JButton btnPrint;}
?它还需要两个辅助类:如下:
public class JRViewerPlus extends JRViewer {protected JButton btnPlus = new javax.swing.JButton();public JRViewerPlus(JasperPrint jrPrint) throws JRException {super(jrPrint);tlbToolBar.remove(btnSave);tlbToolBar.remove(btnReload);btnPlus = new javax.swing.JButton();btnPlus.setToolTipText("Plus...");btnPlus.setText("Plus...");btnPlus.setPreferredSize(new java.awt.Dimension(80, 23));btnPlus.setMaximumSize(new java.awt.Dimension(80, 23));btnPlus.setMinimumSize(new java.awt.Dimension(80, 23));btnPlus.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent evt) {btnPlusActionPerformed(evt);}});tlbToolBar.add(btnPlus, 0);}protected void setZooms() {this.zooms = new int[] { 33, 66, 100, 133, 166, 200, 233 };this.defaultZoomIndex = 2;}protected void btnPlusActionPerformed(java.awt.event.ActionEvent evt) {JOptionPane.showMessageDialog(this,"I just wanted to let you know that you can extend the JRViewer to customize it.\n The button you have pushed was added this way.");}}
?
public class ViewerFrame extends javax.swing.JFrame implementsJRHyperlinkListener {private AppletContext appletContext = null;public ViewerFrame(AppletContext appletContext, JasperPrint jasperPrint)throws JRException {initComponents();this.appletContext = appletContext;JRViewerPlus viewer = new JRViewerPlus(jasperPrint);viewer.addHyperlinkListener(this);this.pnlMain.add(viewer, BorderLayout.CENTER);}public void gotoHyperlink(JRPrintHyperlink hyperlink) {switch (hyperlink.getHyperlinkType()) {case JRHyperlink.HYPERLINK_TYPE_REFERENCE: {try {this.appletContext.showDocument(new URL(hyperlink.getHyperlinkReference()), "_blank");} catch (MalformedURLException e) {JOptionPane.showMessageDialog(this, e.getMessage());}break;}case JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR:case JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE: {break;}case JRHyperlink.HYPERLINK_TYPE_REMOTE_ANCHOR:case JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE: {JOptionPane.showMessageDialog(this,"Implement your own JRHyperlinkListener to manage this type of event.");break;}case JRHyperlink.HYPERLINK_TYPE_NONE:default: {break;}}}private void initComponents() {pnlMain = new javax.swing.JPanel();setTitle("JasperViewer");setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);pnlMain.setLayout(new java.awt.BorderLayout());getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER);pack();java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();setSize(new java.awt.Dimension(400, 300));setLocation((screenSize.width - 400) / 2, (screenSize.height - 300) / 2);}private javax.swing.JPanel pnlMain;}
?
说明:这三个类可以在jasperReport自带的demo中获得,只需做小小的改动就可以了。
?
同样还需要这三个类的编译后的class文件放到WebRoot/applet下面。将jasperreports-3.5.2-applet.jar,commons-logging-1.0.2.jar,commons-collections-2.1.jar三个包加进去。该applet所需要调用的servlet和servlet配置请见:http://zmx.iteye.com/admin/blogs/572812
?
页面调用的applet写法或js动态产生applet如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>打印窗口</title></head><script language="javascript"> <!-- function openView() { var url = "../JRPrintServlet"; document.write('<APPLET ID="JrPrt" CODE = "JRViewApplet.class" CODEBASE = "applets" ARCHIVE = "jasperreports-3.5.2-applet.jar,commons-logging-1.0.2.jar,commons-collections-2.1.jar" WIDTH = "0" HEIGHT = "0">'); document.write('<PARAM NAME = "type" VALUE="application/x-java-applet;version=1.2.2">'); document.write('<PARAM NAME = "scriptable" VALUE="false">'); document.write('<PARAM NAME = "REPORT_URL" VALUE ="'+url+'">'); document.write('</APPLET>'); }--> </script><body bgcolor="#FFFFFF"><input type="button" value="测试预览" onclick="openView();"><!-- <APPLET CODE="JRViewApplet.class" CODEBASE = "applets"ARCHIVE = "jasperreports-3.5.2-applet.jar,commons-logging-1.0.2.jar,commons-collections-2.1.jar"WIDTH="300" HEIGHT="40"></XMP><PARAM NAME=CODE VALUE="JRViewApplet.class"><PARAM NAME = ARCHIVE VALUE = "jasperreports-3.5.2-applet.jar,commons-logging-1.0.2.jar,commons-collections-2.1.jar" ><PARAM NAME="type" VALUE="application/x-java-applet;version=1.2.2"><PARAM NAME="scriptable" VALUE="false"><PARAM NAME="REPORT_URL" VALUE="../JRPrintServlet"></APPLET> --></body></html>
?
1 楼 veteran123 2010-07-26 正需要,研究一下 2 楼 guzhiwxb 2011-10-18 如果是List<JasperPrint> 如何打印预览 3 楼 xietingzi123456 2012-02-16 下载了项目,部署了,跑了, 但是org.apache.jasper.JasperException: An exception occurred processing JSP page /jsp/viewer.jsp at line 18