ar.com.fdvs.dj.core.DJServletHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of DynamicJasper Show documentation
Show all versions of DynamicJasper Show documentation
DynamicJasper (DJ) is an API that hides the complexity of Jasper
Reports, it helps developers to save time when designing
simple/medium complexity reports generating the layout of the
report elements automatically. It creates reports dynamically,
defining at runtime the columns, column width (auto width), groups,
variables, fonts, charts, crosstabs, sub reports (that can also be dynamic), page size
and everything else that you can define at design time.
DJ keeps full compatibility with Jasper Reports since
it's a tool that helps create reports programmatically in
a easy way (it only interferes with the creation of the report
design doing the layout of the elements).
You can use the classic .jrxml files as templates while the
content and layout of the report elements are handled by the DJ
API.
http://dynamicjasper.com
package ar.com.fdvs.dj.core;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;
import ar.com.fdvs.dj.core.layout.LayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.output.ReportWriter;
import ar.com.fdvs.dj.output.ReportWriterFactory;
public class DJServletHelper {
/**
* Generates the report as HTML and setups everything for a clean response (serving images as well).
* You have to declare JasperReport servlet in web.xml (net.sf.jasperreports.j2ee.servlets.ImageServlet)
*
* Web XML must be configured somehow like this:
*
<servlet>
<servlet-name>image</servlet-name>
<servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>image</servlet-name>
<url-pattern>/reports/image</url-pattern>
</servlet-mapping>
*
* @param request
* @param response
* @param imageServletUrl the URI to reach net.sf.jasperreports.j2ee.servlets.ImageServlet servlet (in example it would be "reports/image")
* @param dynamicReport
* @param layoutManager
* @param ds
* @param parameters Parameters for the DynamicReport
* @param exporterParams Extra parameters for JasperReport's HTML exporter (HTMLJRHtmlExporter)
* @throws JRException
* @throws IOException
*/
public static void exportToHtml(HttpServletRequest request,
HttpServletResponse response,
String imageServletUrl,
DynamicReport dynamicReport,
LayoutManager layoutManager,
JRDataSource ds,
Map parameters,
Map exporterParams) throws JRException, IOException
{
if (parameters == null)
parameters = new HashMap();
if (exporterParams == null)
exporterParams = new HashMap();
JasperPrint _jasperPrint = DynamicJasperHelper.generateJasperPrint(dynamicReport, layoutManager, ds,parameters);
final ReportWriter reportWriter = ReportWriterFactory.getInstance().getReportWriter(_jasperPrint, DJConstants.FORMAT_HTML, parameters);
parameters.put(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + imageServletUrl);
Map imagesMap = new HashMap();
JRExporter exporter = reportWriter.getExporter();
exporter.setParameters(exporterParams);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + "/" + imageServletUrl + "?image=");
// Needed to support chart images:
exporter.setParameter(JRExporterParameter.JASPER_PRINT, _jasperPrint);
HttpSession session = request.getSession();
session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, _jasperPrint);
session.setAttribute("net.sf.jasperreports.j2ee.jasper_print", _jasperPrint);
//write generated HTML to the http-response (the one you got from the helper)
reportWriter.writeTo(response);
}
public static void exportToHtml(HttpServletRequest request,
HttpServletResponse response,
String imageServletUrl,
JasperPrint jasperPrint,
Map exporterParams) throws JRException, IOException
{
if (exporterParams == null)
exporterParams = new HashMap();
exporterParams.put(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + imageServletUrl);
final ReportWriter reportWriter = ReportWriterFactory.getInstance().getReportWriter(jasperPrint, DJConstants.FORMAT_HTML, exporterParams);
Map imagesMap = new HashMap();
JRExporter exporter = reportWriter.getExporter();
exporter.setParameters(exporterParams);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + "/" + imageServletUrl + "?image=");
// Needed to support chart images:
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
HttpSession session = request.getSession();
session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
session.setAttribute("net.sf.jasperreports.j2ee.jasper_print", jasperPrint);
//write generated HTML to the http-response (the one you got from the helper)
reportWriter.writeTo(response);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy