to provide basic rendering logic
for views that use a fixed format, e.g. always PDF or always HTML.
| Method from org.springframework.web.servlet.view.jasperreports.AbstractJasperReportsSingleFormatView Detail: |
abstract protected JRExporter createExporter()
|
protected boolean generatesDownloadContent() {
return !useWriter();
}
|
protected Map mergeExporterParameters(Map model) {
Map mergedParameters = new HashMap();
Map convertedExporterParameters = getConvertedExporterParameters();
if (!CollectionUtils.isEmpty(convertedExporterParameters)) {
mergedParameters.putAll(convertedExporterParameters);
}
for (Iterator it = model.keySet().iterator(); it.hasNext();) {
Object key = it.next();
if (key instanceof JRExporterParameter) {
Object value = model.get(key);
Object convertedValue = convertParameterValue((JRExporterParameter) key, value);
mergedParameters.put(key, convertedValue);
}
}
return mergedParameters;
}
Merges the configured JRExporterParameters with any specified in the supplied model data.
JRExporterParameters in the model override those specified in the configuration. |
protected void renderReport(JasperPrint populatedReport,
Map model,
HttpServletResponse response) throws Exception {
JRExporter exporter = createExporter();
// Set exporter parameters - overriding with values from the Model.
Map mergedExporterParameters = mergeExporterParameters(model);
if (!CollectionUtils.isEmpty(mergedExporterParameters)) {
exporter.setParameters(mergedExporterParameters);
}
if (useWriter()) {
renderReportUsingWriter(exporter, populatedReport, response);
}
else {
renderReportUsingOutputStream(exporter, populatedReport, response);
}
}
Perform rendering for a single Jasper Reports exporter, that is,
for a pre-defined output format. |
protected void renderReportUsingOutputStream(JRExporter exporter,
JasperPrint populatedReport,
HttpServletResponse response) throws Exception {
// Apply the content type as specified - we don't need an encoding here.
response.setContentType(getContentType());
// Render report into local OutputStream.
// IE workaround: write into byte array first.
ByteArrayOutputStream baos = new ByteArrayOutputStream(OUTPUT_BYTE_ARRAY_INITIAL_SIZE);
JasperReportsUtils.render(exporter, populatedReport, baos);
// Write content length (determined via byte array).
response.setContentLength(baos.size());
// Flush byte array to servlet output stream.
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
}
We need to write binary output to the response OutputStream. |
protected void renderReportUsingWriter(JRExporter exporter,
JasperPrint populatedReport,
HttpServletResponse response) throws Exception {
// Copy the encoding configured for the report into the response.
String contentType = getContentType();
String encoding = (String) exporter.getParameter(JRExporterParameter.CHARACTER_ENCODING);
if (encoding != null) {
// Only apply encoding if content type is specified but does not contain charset clause already.
if (contentType != null && contentType.toLowerCase().indexOf(WebUtils.CONTENT_TYPE_CHARSET_PREFIX) == -1) {
contentType = contentType + WebUtils.CONTENT_TYPE_CHARSET_PREFIX + encoding;
}
}
response.setContentType(contentType);
// Render report into HttpServletResponse's Writer.
JasperReportsUtils.render(exporter, populatedReport, response.getWriter());
}
We need to write text to the response Writer. |
abstract protected boolean useWriter()
Return whether to use a java.io.Writer to write text content
to the HTTP response. Else, a java.io.OutputStream will be used,
to write binary content to the response. |