org.springframework.web.servlet.view.jasperreports
Class AbstractJasperReportsView

java.lang.Object
  extended by org.springframework.context.support.ApplicationObjectSupport
      extended by org.springframework.web.context.support.WebApplicationObjectSupport
          extended by org.springframework.web.servlet.view.AbstractView
              extended by org.springframework.web.servlet.view.AbstractUrlBasedView
                  extended by org.springframework.web.servlet.view.jasperreports.AbstractJasperReportsView
All Implemented Interfaces:
BeanNameAware, InitializingBean, ApplicationContextAware, ServletContextAware, View
Direct Known Subclasses:
AbstractJasperReportsSingleFormatView, JasperReportsMultiFormatView

public abstract class AbstractJasperReportsView
extends AbstractUrlBasedView

Base class for all JasperReports views. Applies on-the-fly compilation of report designs as required and coordinates the rendering process. The resource path of the main report needs to be specified as url.

This class is responsible for getting report data from the model that has been provided to the view. The default implementation checks for a model object under the specified reportDataKey first, then falls back to looking for a value of type JRDataSource, java.util.Collection, object array (in that order).

If no JRDataSource can be found in the model, then reports will be filled using the configured javax.sql.DataSource if any. If neither a JRDataSource or javax.sql.DataSource is available then an IllegalArgumentException is raised.

Provides support for sub-reports through the subReportUrls and subReportDataKeys properties.

When using sub-reports, the master report should be configured using the url property and the sub-reports files should be configured using the subReportUrls property. Each entry in the subReportUrls Map corresponds to an individual sub-report. The key of an entry must match up to a sub-report parameter in your report file of type net.sf.jasperreports.engine.JasperReport, and the value of an entry must be the URL for the sub-report file.

For sub-reports that require an instance of JRDataSource, that is, they don't have a hard-coded query for data retrieval, you can include the appropriate data in your model as would with the data source for the parent report. However, you must provide a List of parameter names that need to be converted to JRDataSource instances for the sub-report via the subReportDataKeys property. When using JRDataSource instances for sub-reports, you must specify a value for the reportDataKey property, indicating the data to use for the main report.

Allows for exporter parameters to be configured declatively using the exporterParameters property. This is a Map typed property where the key of an entry corresponds to the fully-qualified name of the static field for the JRExporterParameter and the value of an entry is the value you want to assign to the exporter parameter.

Response headers can be controlled via the headers property. Spring will attempt to set the correct value for the Content-Diposition header so that reports render correctly in Internet Explorer. However, you can override this setting through the headers property.

Since:
1.1.3
Author:
Rob Harrop, Juergen Hoeller
See Also:
AbstractUrlBasedView.setUrl(java.lang.String), setReportDataKey(java.lang.String), setSubReportUrls(java.util.Properties), setSubReportDataKeys(java.lang.String[]), setHeaders(java.util.Properties), setExporterParameters(java.util.Map), setJdbcDataSource(javax.sql.DataSource)

Field Summary
protected static java.lang.String CONTENT_DISPOSITION_INLINE
          The default Content-Disposition header.
private  java.util.Map<net.sf.jasperreports.engine.JRExporterParameter,java.lang.Object> convertedExporterParameters
          Stores the converted exporter parameters - keyed by JRExporterParameter.
private  java.util.Map<?,?> exporterParameters
          Stores the exporter parameters passed in by the user as passed in by the user.
protected static java.lang.String HEADER_CONTENT_DISPOSITION
          Constant that defines "Content-Disposition" header.
private  java.util.Properties headers
          Stores the headers to written with each response
private  javax.sql.DataSource jdbcDataSource
          Stores the DataSource, if any, used as the report data source.
private  net.sf.jasperreports.engine.JasperReport report
          The JasperReport that is used to render the view.
private  java.lang.String reportDataKey
          A String key used to lookup the JRDataSource in the model.
private  java.lang.String[] subReportDataKeys
          Stores the names of any data source objects that need to be converted to JRDataSource instances and included in the report parameters to be passed on to a sub-report.
private  java.util.Map<java.lang.String,net.sf.jasperreports.engine.JasperReport> subReports
          Holds mappings between sub-report keys and JasperReport objects.
private  java.util.Properties subReportUrls
          Stores the paths to any sub-report files used by this top-level report, along with the keys they are mapped to in the top-level report file.
 
Fields inherited from class org.springframework.web.servlet.view.AbstractView
DEFAULT_CONTENT_TYPE
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Fields inherited from interface org.springframework.web.servlet.View
RESPONSE_STATUS_ATTRIBUTE
 
Constructor Summary
AbstractJasperReportsView()
           
 
Method Summary
protected  void convertExporterParameters()
          Converts the exporter parameters passed in by the user which may be keyed by Strings corresponding to the fully qualified name of the JRExporterParameter into parameters which are keyed by JRExporterParameter.
protected  java.lang.Object convertParameterValue(net.sf.jasperreports.engine.JRExporterParameter parameter, java.lang.Object value)
          Convert the supplied parameter value into the actual type required by the corresponding JRExporterParameter.
protected  net.sf.jasperreports.engine.JRDataSource convertReportData(java.lang.Object value)
          Convert the given report data value to a JRDataSource.
protected  net.sf.jasperreports.engine.JRExporterParameter convertToExporterParameter(java.lang.String fqFieldName)
          Convert the given fully qualified field name to a corresponding JRExporterParameter instance.
protected  net.sf.jasperreports.engine.JRDataSource createReport(net.sf.jasperreports.engine.JRDataSourceProvider provider)
          Create a report using the given provider.
private  net.sf.jasperreports.engine.JasperPrint doFillReport(net.sf.jasperreports.engine.JasperReport report, java.util.Map<java.lang.String,java.lang.Object> model, javax.sql.DataSource ds)
          Fill the given report using the given JDBC DataSource and model.
protected  void exposeLocalizationContext(java.util.Map<java.lang.String,java.lang.Object> model, javax.servlet.http.HttpServletRequest request)
          Expose current Spring-managed Locale and MessageSource to JasperReports i18n ($R expressions etc).
protected  net.sf.jasperreports.engine.JasperPrint fillReport(java.util.Map<java.lang.String,java.lang.Object> model)
          Create a populated JasperPrint instance from the configured JasperReport instance.
protected  java.util.Map<net.sf.jasperreports.engine.JRExporterParameter,java.lang.Object> getConvertedExporterParameters()
          Allows subclasses to retrieve the converted exporter parameters.
protected  net.sf.jasperreports.engine.JRExporterParameter getExporterParameter(java.lang.Object parameter)
          Return a JRExporterParameter for the given parameter object, converting it from a String if necessary.
 java.util.Map<?,?> getExporterParameters()
          Return the exporter parameters that this view uses, if any.
protected  javax.sql.DataSource getJdbcDataSource()
          Return the javax.sql.DataSource that this view uses, if any.
protected  net.sf.jasperreports.engine.JasperReport getReport()
          Determine the JasperReport to fill.
protected  net.sf.jasperreports.engine.JRDataSource getReportData(java.util.Map<java.lang.String,java.lang.Object> model)
          Create an appropriate JRDataSource for passed-in report data.
protected  java.lang.Class[] getReportDataTypes()
          Return the value types that can be converted to a JRDataSource, in prioritized order.
protected  void initApplicationContext()
          Checks to see that a valid report file URL is supplied in the configuration.
protected  boolean isUrlRequired()
          JasperReports views do not strictly required a 'url' value.
protected  net.sf.jasperreports.engine.JasperReport loadReport()
          Load the main JasperReport from the specified Resource.
protected  net.sf.jasperreports.engine.JasperReport loadReport(Resource resource)
          Loads a JasperReport from the specified Resource.
protected  void onInit()
          Subclasses can override this to add some custom initialization logic.
private  void populateHeaders(javax.servlet.http.HttpServletResponse response)
          Populates the headers in the HttpServletResponse with the headers supplied by the user.
protected  void postProcessReport(net.sf.jasperreports.engine.JasperPrint populatedReport, java.util.Map<java.lang.String,java.lang.Object> model)
          Template method to be overridden for custom post-processing of the populated report.
protected  void renderMergedOutputModel(java.util.Map<java.lang.String,java.lang.Object> model, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Finds the report data to use for rendering the report and then invokes the renderReport(net.sf.jasperreports.engine.JasperPrint, java.util.Map, javax.servlet.http.HttpServletResponse) method that should be implemented by the subclass.
protected abstract  void renderReport(net.sf.jasperreports.engine.JasperPrint populatedReport, java.util.Map<java.lang.String,java.lang.Object> model, javax.servlet.http.HttpServletResponse response)
          Subclasses should implement this method to perform the actual rendering process.
protected  void setConvertedExporterParameters(java.util.Map<net.sf.jasperreports.engine.JRExporterParameter,java.lang.Object> convertedExporterParameters)
          Allows subclasses to populate the converted exporter parameters.
 void setExporterParameters(java.util.Map<?,?> parameters)
          Set the exporter parameters that should be used when rendering a view.
 void setHeaders(java.util.Properties headers)
          Specify the set of headers that are included in each of response.
 void setJdbcDataSource(javax.sql.DataSource jdbcDataSource)
          Specify the javax.sql.DataSource to use for reports with embedded SQL statements.
 void setReportDataKey(java.lang.String reportDataKey)
          Set the name of the model attribute that represents the report data.
 void setSubReportDataKeys(java.lang.String[] subReportDataKeys)
          Set the list of names corresponding to the model parameters that will contain data source objects for use in sub-reports.
 void setSubReportUrls(java.util.Properties subReports)
          Specify resource paths which must be loaded as instances of JasperReport and passed to the JasperReports engine for rendering as sub-reports, under the same keys as in this mapping.
 
Methods inherited from class org.springframework.web.servlet.view.AbstractUrlBasedView
afterPropertiesSet, checkResource, getUrl, setUrl, toString
 
Methods inherited from class org.springframework.web.servlet.view.AbstractView
addStaticAttribute, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setRequestContextAttribute, writeToResponse
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
 
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, requiredContextClass, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

HEADER_CONTENT_DISPOSITION

protected static final java.lang.String HEADER_CONTENT_DISPOSITION
Constant that defines "Content-Disposition" header.

See Also:
Constant Field Values

CONTENT_DISPOSITION_INLINE

protected static final java.lang.String CONTENT_DISPOSITION_INLINE
The default Content-Disposition header. Used to make IE play nice.

See Also:
Constant Field Values

reportDataKey

private java.lang.String reportDataKey
A String key used to lookup the JRDataSource in the model.


subReportUrls

private java.util.Properties subReportUrls
Stores the paths to any sub-report files used by this top-level report, along with the keys they are mapped to in the top-level report file.


subReportDataKeys

private java.lang.String[] subReportDataKeys
Stores the names of any data source objects that need to be converted to JRDataSource instances and included in the report parameters to be passed on to a sub-report.


headers

private java.util.Properties headers
Stores the headers to written with each response


exporterParameters

private java.util.Map<?,?> exporterParameters
Stores the exporter parameters passed in by the user as passed in by the user. May be keyed as Strings with the fully qualified name of the exporter parameter field.


convertedExporterParameters

private java.util.Map<net.sf.jasperreports.engine.JRExporterParameter,java.lang.Object> convertedExporterParameters
Stores the converted exporter parameters - keyed by JRExporterParameter.


jdbcDataSource

private javax.sql.DataSource jdbcDataSource
Stores the DataSource, if any, used as the report data source.


report

private net.sf.jasperreports.engine.JasperReport report
The JasperReport that is used to render the view.


subReports

private java.util.Map<java.lang.String,net.sf.jasperreports.engine.JasperReport> subReports
Holds mappings between sub-report keys and JasperReport objects.

Constructor Detail

AbstractJasperReportsView

public AbstractJasperReportsView()