I folks,
I have followed this conversation http://forum.springframework.org/vie...ghlight=jasper to some time, and would like to contribute of any form.
I tired to try to understand because the JasperReports present different layouts when presenting reports in HTML for different browsers. I tried to use the Acrobat Reader to facilitate this, but many customers had not liked the idea and at some moments the report was not presented. Then I saw that the version 0.6.4 I could use an applet to present the report.
I studed the idea and see that the applet needed a JasperPrint serialized in the reponse stream and the Spring solution at the moment don't support it.
Finally I make an public class JasperReportsPrintView extends AbstractJasperReportsSingleFormatView to solve this, and I would like to share the idea with yours.
and for a context overviewCode:package ...presentation.web.support.springframework;
import java.io.ObjectOutputStream;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRAbstractExporter;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import org.springframework.web.servlet.view.jasperreports.AbstractJasperReportsSingleFormatView;
public class JasperReportsPrintView extends
AbstractJasperReportsSingleFormatView {
/**
* Default Constructor
*/
public JasperReportsPrintView() {
setContentType("application/octet-stream");
}
/**
* Perform rendering for a single Jasper Reports exporter,
* i.e. a pre-defined output format.
*/
protected void renderReport(
JasperReport report, Map parameters, JRDataSource dataSource, HttpServletResponse response)
throws Exception {
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, dataSource);
ServletOutputStream ouputStream = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
oos.writeObject(jasperPrint);
oos.flush();
oos.close();
ouputStream.flush();
ouputStream.close();
}
/**
* @see org.springframework.web.servlet.view.jasperreports.AbstractJasperReportsSingleFormatView#createExporter()
*/
protected JRAbstractExporter createExporter() {
return null;
}
/**
* @see org.springframework.web.servlet.view.jasperreports.AbstractJasperReportsSingleFormatView#useWriter()
*/
protected boolean useWriter() {
return false;
}
}
1) The servlet report config:
The jasper.propeties file:Code:<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="reportController"
class="...reportController"
autowire="byName">
<property name="facade">
<ref bean="businessDelegate" />
</property>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="**/report.jasper">reportController</prop>
</props>
</property>
</bean>
<bean id="jasperReportsViewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename">
<value>jasper</value>
</property>
</bean>
</beans>
The report Controller class:Code:##############################
# JasperReports View Classes
##############################
# org.springframework.web.servlet.view.jasperreports.JasperReportsCsvView
# org.springframework.web.servlet.view.jasperreports.JasperReportsHtmlView
# org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView
# org.springframework.web.servlet.view.jasperreports.JasperReportsXlsView
################
# Reports
################
report.class=...presentation.web.support.springframework.JasperReportsPrintView
report.url=/WEB-INF/reports/Report.jrxml
report.reportDataKey=reportData
The EmbeddedViewerApplet class:Code:package ...presentation.web.classes.owner.report;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import ...facade.BusinessDelegate;
import ...presentation.web.support.WebSupportUtils;
import ...presentation.web.support.jasper.JRHibernateQueryResultDataSource;
public class ReportController implements Controller {
protected BusinessDelegate facade;
/**
*
* @param facade BusinessDelegate
*/
public void setFacade(BusinessDelegate facade) {
this.facade = facade;
}
/**
*
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws Exception
* @return ModelAndView
*/
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
Collection dataSource = facade.ownerReport(
WebSupportUtils.getAuthenticatedOwner(request.getSession(), true));
String[] fields = new String[] {"field1",
"field2",
"field3",
"field4",
"field5",
"field6",
"field7"};
Map model = new HashMap();
ServletContext context = request.getSession().getServletContext();
model.put("framesup_img01", context.getRealPath("imagens/framesup_img01.jpg"));
model.put("framesup_img02", context.getRealPath("imagens/framesup_img02.jpg"));
model.put("framesup_img03", context.getRealPath("imagens/framesup_img03.jpg"));
model.put("reportData", new JRHibernateQueryResultDataSource(dataSource, fields));
return new ModelAndView("report", model);
}
}
The JSP report applet viewer:Code:/*
* ============================================================================
* The JasperReports License, Version 1.0
* ============================================================================
*
* Copyright (C) 2001-2004 Teodor Danciu (teodord@users.sourceforge.net). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
* include the following acknowledgment: "This product includes software
* developed by Teodor Danciu (http://jasperreports.sourceforge.net)."
* Alternately, this acknowledgment may appear in the software itself, if
* and wherever such third-party acknowledgments normally appear.
*
* 4. The name "JasperReports" must not be used to endorse or promote products
* derived from this software without prior written permission. For written
* permission, please contact teodord@users.sourceforge.net.
*
* 5. Products derived from this software may not be called "JasperReports", nor
* may "JasperReports" appear in their name, without prior written permission
* of Teodor Danciu.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
* DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* ============================================================================
* GNU Lesser General Public License
* ============================================================================
*
* JasperReports - Free Java report-generating library.
* Copyright (C) 2001-2004 Teodor Danciu teodord@users.sourceforge.net
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Teodor Danciu
* 173, Calea Calarasilor, Bl. 42, Sc. 1, Ap. 18
* Postal code 030615, Sector 3
* Bucharest, ROMANIA
* Email: teodord@users.sourceforge.net
*/
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.util.*;
import net.sf.jasperreports.view.*;
import java.io.*;
import java.net.*;
import java.awt.BorderLayout;
import javax.swing.JOptionPane;
/**
* @author Teodor Danciu (teodord@users.sourceforge.net)
* @version $Id: EmbeddedViewerApplet.java,v 1.4 2004/06/01 20:46:04 teodord Exp $
*/
public class EmbeddedViewerApplet extends javax.swing.JApplet
{
/**
*
*/
private JasperPrint jasperPrint = null;
/** Creates new form AppletViewer */
public EmbeddedViewerApplet()
{
initComponents();
}
/**
*
*/
public void init()
{
String url = getParameter("REPORT_URL");
if (url != null)
{
try
{
jasperPrint = (JasperPrint)JRLoader.loadObject(new URL(getCodeBase(), url));
if (jasperPrint != null)
{
JRViewerSimple viewer = new JRViewerSimple(jasperPrint);
this.pnlMain.add(viewer, BorderLayout.CENTER);
}
}
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");
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
pnlMain = new javax.swing.JPanel();
pnlMain.setLayout(new java.awt.BorderLayout());
getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER);
}//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel pnlMain;
// End of variables declaration//GEN-END:variables
}
1) The applet make the /site/report.jasper request;Code:<%@ page errorPage="/layouts/erro.jsp" %>
<%
String appletWidth = "750";
String appletHeight = "600";
%>
<html>
<head>
<title>Report Viewer</title>
</head>
<body BGCOLOR="#ffffff" LINK="#000099">
<!--"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="<%= appletWidth %>" HEIGHT="<%= appletHeight %>" codebase="http://java.com/en/download/windows_automatic.jsp"><NOEMBED><XMP>');
else if (_ns == true && _ns6 == false) document.writeln('<EMBED type="application/x-java-applet;version=1.1.2" CODE = "EmbeddedViewerApplet.class" CODEBASE = "applet" ARCHIVE = "jasperreports-0.6.4-applet.jar" WIDTH="<%= appletWidth %>" HEIGHT="<%= appletHeight %>" REPORT_URL = "<%= request.getParameter("REPORT_URL") %>" scriptable=false pluginspage="http://java.com/en/download/windows_automatic.jsp"><NOEMBED><XMP>');
//--></SCRIPT>
<APPLET CODE="EmbeddedViewerApplet.class" CODEBASE="applet"
ARCHIVE="jasperreports-0.6.4-applet.jar" WIDTH="<%= appletWidth %>" HEIGHT="<%= appletHeight %>"></XMP>
<PARAM NAME=CODE VALUE="EmbeddedViewerApplet.class">
<PARAM NAME=CODEBASE VALUE="applet">
<PARAM NAME=ARCHIVE VALUE="jasperreports-0.6.4-applet.jar">
<PARAM NAME="type" VALUE="application/x-java-applet;version=1.2.2">
<PARAM NAME="scriptable" VALUE="false">
<PARAM NAME="REPORT_URL" VALUE="<%= request.getParameter("REPORT_URL") %>">
</APPLET>
</NOEMBED>
</EMBED>
</OBJECT>
<!--
<APPLET CODE = "EmbeddedViewerApplet.class" CODEBASE = "applet" ARCHIVE = "jasperreports-0.6.4-applet.jar" WIDTH="<%= appletWidth %>" HEIGHT="<%= appletHeight %>">
<PARAM NAME = "REPORT_URL" VALUE ="<%= request.getParameter("REPORT_URL") %>">
</APPLET>
-->
<!--"END_CONVERTED_APPLET"-->
</body>
</html>
2) The ReportController handle the request
3) The JasperReportsPrintView intercepts, compile and return the JasperPrint object to the applet
TIA
