public class JasperReportsMultiFormatView extends AbstractJasperReportsView
This view works on the concept of a format key and a mapping key.
The format key is used to pass the mapping key from your
Controller
to Spring through as part of the model and the
mapping key is used to map a logical format to an actual JasperReports
view class. For example you might add the following code to your
Controller
:
MapHeremodel = new HashMap (); model.put("format", "pdf");
format
is the format key and pdf
is
the mapping key. When rendering a report, this class looks for a
model parameter under the format key, which by default is
format
. It then uses the value of this parameter to lookup
the actual View
class to use. The default mappings for this
lookup are:
csv
- JasperReportsCsvView
html
- JasperReportsHtmlView
pdf
- JasperReportsPdfView
xls
- JasperReportsXlsView
The format key can be changed using the formatKey
property and the mapping key to view class mappings can be changed using the
formatMappings
property.
setFormatKey(java.lang.String)
,
setFormatMappings(java.util.Map<java.lang.String, java.lang.Class<? extends org.springframework.web.servlet.view.jasperreports.AbstractJasperReportsView>>)
Modifier and Type | Field and Description |
---|---|
private java.util.Properties |
contentDispositionMappings
Stores the mappings of mapping keys to Content-Disposition header values.
|
static java.lang.String |
DEFAULT_FORMAT_KEY
Default value used for format key: "format"
|
private java.lang.String |
formatKey
The key of the model parameter that holds the format key.
|
private java.util.Map<java.lang.String,java.lang.Class<? extends AbstractJasperReportsView>> |
formatMappings
Stores the format mappings, with the format discriminator
as key and the corresponding view class as value.
|
CONTENT_DISPOSITION_INLINE, HEADER_CONTENT_DISPOSITION
DEFAULT_CONTENT_TYPE
logger
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
Constructor and Description |
---|
JasperReportsMultiFormatView()
Creates a new
JasperReportsMultiFormatView instance
with a default set of mappings. |
Modifier and Type | Method and Description |
---|---|
protected boolean |
generatesDownloadContent()
Return whether this view generates download content
(typically binary content like PDF or Excel files).
|
java.util.Properties |
getContentDispositionMappings()
Return the mappings of
Content-Disposition header values to
mapping keys. |
private void |
populateContentDispositionIfNecessary(HttpServletResponse response,
java.lang.String format)
Adds/overwrites the
Content-Disposition header value with the format-specific
value if the mappings have been specified and a valid one exists for the given format. |
protected void |
renderReport(JasperPrint populatedReport,
java.util.Map<java.lang.String,java.lang.Object> model,
HttpServletResponse response)
Locates the format key in the model using the configured discriminator key and uses this
key to lookup the appropriate view class from the mappings.
|
void |
setContentDispositionMappings(java.util.Properties mappings)
Set the mappings of
Content-Disposition header values to
mapping keys. |
void |
setFormatKey(java.lang.String formatKey)
Set the key of the model parameter that holds the format discriminator.
|
void |
setFormatMappings(java.util.Map<java.lang.String,java.lang.Class<? extends AbstractJasperReportsView>> formatMappings)
Set the mappings of format discriminators to view class names.
|
convertExporterParameters, convertParameterValue, convertReportData, convertToExporterParameter, createReport, exposeLocalizationContext, fillReport, getConvertedExporterParameters, getExporterParameter, getExporterParameters, getJdbcDataSource, getReport, getReportData, getReportDataTypes, initApplicationContext, isUrlRequired, loadReport, loadReport, onInit, postProcessReport, renderMergedOutputModel, setConvertedExporterParameters, setExporterParameters, setHeaders, setJdbcDataSource, setReportDataKey, setSubReportDataKeys, setSubReportUrls
afterPropertiesSet, checkResource, getUrl, setUrl, toString
addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, isExposePathVariables, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposePathVariables, setRequestContextAttribute, setResponseContentType, writeToResponse
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, requiredContextClass, setApplicationContext
public static final java.lang.String DEFAULT_FORMAT_KEY
private java.lang.String formatKey
private java.util.Map<java.lang.String,java.lang.Class<? extends AbstractJasperReportsView>> formatMappings
private java.util.Properties contentDispositionMappings
public JasperReportsMultiFormatView()
JasperReportsMultiFormatView
instance
with a default set of mappings.public void setFormatKey(java.lang.String formatKey)
public void setFormatMappings(java.util.Map<java.lang.String,java.lang.Class<? extends AbstractJasperReportsView>> formatMappings)
csv
- JasperReportsCsvView
html
- JasperReportsHtmlView
pdf
- JasperReportsPdfView
xls
- JasperReportsXlsView
public void setContentDispositionMappings(java.util.Properties mappings)
Content-Disposition
header values to
mapping keys. If specified, Spring will look at these mappings to determine
the value of the Content-Disposition
header for a given
format mapping.public java.util.Properties getContentDispositionMappings()
Content-Disposition
header values to
mapping keys. Mainly available for configuration through property paths
that specify individual keys.protected boolean generatesDownloadContent()
AbstractView
The default implementation returns false
. Subclasses are
encouraged to return true
here if they know that they are
generating download content that requires temporary caching on the
client side, typically via the response OutputStream.
generatesDownloadContent
in class AbstractView
AbstractView.prepareResponse(HttpServletRequest, HttpServletResponse)
,
javax.servlet.http.HttpServletResponse#getOutputStream()
protected void renderReport(JasperPrint populatedReport, java.util.Map<java.lang.String,java.lang.Object> model, HttpServletResponse response) throws java.lang.Exception
renderReport
in class AbstractJasperReportsView
populatedReport
- the populated JasperPrint
to rendermodel
- the map containing report parametersresponse
- the HTTP response the report should be rendered tojava.lang.Exception
- if rendering failedAbstractView.getContentType()
,
javax.servlet.ServletResponse#setContentType
,
javax.servlet.ServletResponse#setCharacterEncoding
private void populateContentDispositionIfNecessary(HttpServletResponse response, java.lang.String format)
Content-Disposition
header value with the format-specific
value if the mappings have been specified and a valid one exists for the given format.response
- the HttpServletResponse
to set the header informat
- the format key of the mappingsetContentDispositionMappings(java.util.Properties)