类 AbstractJasperReportsView
- java.lang.Object
- org.springframework.context.support.ApplicationObjectSupport
- org.springframework.web.context.support.WebApplicationObjectSupport
- org.springframework.web.servlet.view.AbstractView
- org.springframework.web.servlet.view.AbstractUrlBasedView
- org.springframework.web.servlet.view.jasperreports.AbstractJasperReportsView
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 asurl.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
reportDataKeyfirst, then falls back to looking for a value of typeJRDataSource,java.util.Collection, object array (in that order).If no
JRDataSourcecan be found in the model, then reports will be filled using the configuredjavax.sql.DataSourceif any. If neither aJRDataSourceorjavax.sql.DataSourceis available then anIllegalArgumentExceptionis raised.Provides support for sub-reports through the
subReportUrlsandsubReportDataKeysproperties.When using sub-reports, the master report should be configured using the
urlproperty and the sub-reports files should be configured using thesubReportUrlsproperty. Each entry in thesubReportUrlsMap corresponds to an individual sub-report. The key of an entry must match up to a sub-report parameter in your report file of typenet.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 toJRDataSourceinstances for the sub-report via thesubReportDataKeysproperty. When usingJRDataSourceinstances for sub-reports, you must specify a value for thereportDataKeyproperty, indicating the data to use for the main report.Allows for exporter parameters to be configured declatively using the
exporterParametersproperty. This is aMaptyped property where the key of an entry corresponds to the fully-qualified name of the static field for theJRExporterParameterand the value of an entry is the value you want to assign to the exporter parameter.Response headers can be controlled via the
headersproperty. Spring will attempt to set the correct value for theContent-Dipositionheader so that reports render correctly in Internet Explorer. However, you can override this setting through theheadersproperty.This class is compatible with classic JasperReports releases back until 2.x. As a consequence, it keeps using the
JRExporterAPI which got deprecated as of JasperReports 5.5.2 (early 2014).- 从以下版本开始:
- 1.1.3
- 作者:
- Rob Harrop, Juergen Hoeller
- 另请参阅:
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)
字段概要
字段 修饰符和类型 字段 说明 protected static StringCONTENT_DISPOSITION_INLINEThe default Content-Disposition header.protected static StringHEADER_CONTENT_DISPOSITIONConstant that defines "Content-Disposition" header.从类继承的字段 org.springframework.web.servlet.view.AbstractView
DEFAULT_CONTENT_TYPE
从类继承的字段 org.springframework.context.support.ApplicationObjectSupport
logger
从接口继承的字段 org.springframework.web.servlet.View
PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE
构造器概要
构造器 构造器 说明 AbstractJasperReportsView()
方法概要
所有方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 protected voidconvertExporterParameters()Converts the exporter parameters passed in by the user which may be keyed byStrings corresponding to the fully qualified name of theJRExporterParameterinto parameters which are keyed byJRExporterParameter.protected ObjectconvertParameterValue(net.sf.jasperreports.engine.JRExporterParameter parameter, Object value)Convert the supplied parameter value into the actual type required by the correspondingJRExporterParameter.protected net.sf.jasperreports.engine.JRDataSourceconvertReportData(Object value)Convert the given report data value to aJRDataSource.protected net.sf.jasperreports.engine.JRExporterParameterconvertToExporterParameter(String fqFieldName)Convert the given fully qualified field name to a corresponding JRExporterParameter instance.protected net.sf.jasperreports.engine.JRDataSourcecreateReport(net.sf.jasperreports.engine.JRDataSourceProvider provider)Create a report using the given provider.protected voidexposeLocalizationContext(Map<String,Object> model, HttpServletRequest request)Expose current Spring-managed Locale and MessageSource to JasperReports i18n ($R expressions etc).protected net.sf.jasperreports.engine.JasperPrintfillReport(Map<String,Object> model)Create a populatedJasperPrintinstance from the configuredJasperReportinstance.protected Map<net.sf.jasperreports.engine.JRExporterParameter,Object>getConvertedExporterParameters()Allows subclasses to retrieve the converted exporter parameters.protected net.sf.jasperreports.engine.JRExporterParametergetExporterParameter(Object parameter)Return aJRExporterParameterfor the given parameter object, converting it from a String if necessary.Map<?,?>getExporterParameters()Return the exporter parameters that this view uses, if any.protected DataSourcegetJdbcDataSource()Return thejavax.sql.DataSourcethat this view uses, if any.protected net.sf.jasperreports.engine.JasperReportgetReport()Determine theJasperReportto fill.protected net.sf.jasperreports.engine.JRDataSourcegetReportData(Map<String,Object> model)Create an appropriateJRDataSourcefor passed-in report data.protected Class<?>[]getReportDataTypes()Return the value types that can be converted to aJRDataSource, in prioritized order.protected voidinitApplicationContext()Checks to see that a valid report file URL is supplied in the configuration.protected booleanisUrlRequired()JasperReports views do not strictly required a 'url' value.protected net.sf.jasperreports.engine.JasperReportloadReport()Load the mainJasperReportfrom the specifiedResource.protected net.sf.jasperreports.engine.JasperReportloadReport(Resource resource)Loads aJasperReportfrom the specifiedResource.protected voidonInit()Subclasses can override this to add some custom initialization logic.protected voidpostProcessReport(net.sf.jasperreports.engine.JasperPrint populatedReport, Map<String,Object> model)Template method to be overridden for custom post-processing of the populated report.protected voidrenderMergedOutputModel(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response)Finds the report data to use for rendering the report and then invokes therenderReport(net.sf.jasperreports.engine.JasperPrint, java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletResponse)method that should be implemented by the subclass.protected abstract voidrenderReport(net.sf.jasperreports.engine.JasperPrint populatedReport, Map<String,Object> model, HttpServletResponse response)Subclasses should implement this method to perform the actual rendering process.protected voidsetConvertedExporterParameters(Map<net.sf.jasperreports.engine.JRExporterParameter,Object> parameters)Allows subclasses to populate the converted exporter parameters.voidsetExporterParameters(Map<?,?> parameters)Set the exporter parameters that should be used when rendering a view.voidsetHeaders(Properties headers)Specify the set of headers that are included in each of response.voidsetJdbcDataSource(DataSource jdbcDataSource)Specify thejavax.sql.DataSourceto use for reports with embedded SQL statements.voidsetReportDataKey(String reportDataKey)Set the name of the model attribute that represents the report data.voidsetSubReportDataKeys(String... subReportDataKeys)Set the list of names corresponding to the model parameters that will contain data source objects for use in sub-reports.voidsetSubReportUrls(Properties subReports)Specify resource paths which must be loaded as instances ofJasperReportand passed to the JasperReports engine for rendering as sub-reports, under the same keys as in this mapping.从类继承的方法 org.springframework.web.servlet.view.AbstractUrlBasedView
afterPropertiesSet, checkResource, getUrl, setUrl, toString
从类继承的方法 org.springframework.web.servlet.view.AbstractView
addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getRequestToExpose, getStaticAttributes, isExposePathVariables, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setRequestContextAttribute, setResponseContentType, writeToResponse
从类继承的方法 org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
从类继承的方法 org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, requiredContextClass, setApplicationContext
字段详细资料
HEADER_CONTENT_DISPOSITION
protected static final String HEADER_CONTENT_DISPOSITION
Constant that defines "Content-Disposition" header.- 另请参阅:
- 常量字段值
CONTENT_DISPOSITION_INLINE
protected static final String CONTENT_DISPOSITION_INLINE
The default Content-Disposition header. Used to make IE play nice.- 另请参阅:
- 常量字段值
构造器详细资料
AbstractJasperReportsView
public AbstractJasperReportsView()
方法详细资料
setReportDataKey
public void setReportDataKey(String reportDataKey)
Set the name of the model attribute that represents the report data. If not specified, the model map will be searched for a matching value type.A
JRDataSourcewill be taken as-is. For other types, conversion will apply: By default, ajava.util.Collectionwill be converted toJRBeanCollectionDataSource, and an object array toJRBeanArrayDataSource.Note: If you pass in a Collection or object array in the model map for use as plain report parameter, rather than as report data to extract fields from, you need to specify the key for the actual report data to use, to avoid mis-detection of report data by type.
- 另请参阅:
convertReportData(java.lang.Object),JRDataSource,JRBeanCollectionDataSource,JRBeanArrayDataSource
setSubReportUrls
public void setSubReportUrls(Properties subReports)
Specify resource paths which must be loaded as instances ofJasperReportand passed to the JasperReports engine for rendering as sub-reports, under the same keys as in this mapping.- 参数:
subReports- mapping between model keys and resource paths (Spring resource locations)- 另请参阅:
AbstractUrlBasedView.setUrl(java.lang.String),ResourceLoader.getResource(java.lang.String)
setSubReportDataKeys
public void setSubReportDataKeys(String... subReportDataKeys)
Set the list of names corresponding to the model parameters that will contain data source objects for use in sub-reports. Spring will convert these objects to instances ofJRDataSourcewhere applicable and will then include the resultingJRDataSourcein the parameters passed into the JasperReports engine.The name specified in the list should correspond to an attribute in the model Map, and to a sub-report data source parameter in your report file. If you pass in
JRDataSourceobjects as model attributes, specifying this list of keys is not required.If you specify a list of sub-report data keys, it is required to also specify a
reportDataKeyfor the main report, to avoid confusion between the data source objects for the various reports involved.- 参数:
subReportDataKeys- list of names for sub-report data source objects- 另请参阅:
setReportDataKey(java.lang.String),convertReportData(java.lang.Object),JRDataSource,JRBeanCollectionDataSource,JRBeanArrayDataSource
setHeaders
public void setHeaders(Properties headers)
Specify the set of headers that are included in each of response.- 参数:
headers- the headers to write to each response.
setExporterParameters
public void setExporterParameters(Map<?,?> parameters)
Set the exporter parameters that should be used when rendering a view.- 参数:
parameters-Mapwith the fully qualified field name of theJRExporterParameterinstance as key (e.g. "net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI") and the value you wish to assign to the parameter as value
getExporterParameters
public Map<?,?> getExporterParameters()
Return the exporter parameters that this view uses, if any.
setConvertedExporterParameters
protected void setConvertedExporterParameters(Map<net.sf.jasperreports.engine.JRExporterParameter,Object> parameters)
Allows subclasses to populate the converted exporter parameters.
getConvertedExporterParameters
protected Map<net.sf.jasperreports.engine.JRExporterParameter,Object> getConvertedExporterParameters()
Allows subclasses to retrieve the converted exporter parameters.
setJdbcDataSource
public void setJdbcDataSource(DataSource jdbcDataSource)
Specify thejavax.sql.DataSourceto use for reports with embedded SQL statements.
getJdbcDataSource
protected DataSource getJdbcDataSource()
Return thejavax.sql.DataSourcethat this view uses, if any.
isUrlRequired
protected boolean isUrlRequired()
JasperReports views do not strictly required a 'url' value. Alternatively, thegetReport()template method may be overridden.
initApplicationContext
protected final void initApplicationContext() throws ApplicationContextException
Checks to see that a valid report file URL is supplied in the configuration. Compiles the report file is necessary.Subclasses can add custom initialization logic by overriding the
onInit()method.- 覆盖:
initApplicationContext在类中ApplicationObjectSupport- 抛出:
ApplicationContextException- in case of initialization errors- 另请参阅:
ApplicationObjectSupport.setApplicationContext(org.springframework.context.ApplicationContext)
onInit
protected void onInit()
Subclasses can override this to add some custom initialization logic. Called byinitApplicationContext()as soon as all standard initialization logic has finished executing.
convertExporterParameters
protected final void convertExporterParameters()
Converts the exporter parameters passed in by the user which may be keyed byStrings corresponding to the fully qualified name of theJRExporterParameterinto parameters which are keyed byJRExporterParameter.
convertParameterValue
protected Object convertParameterValue(net.sf.jasperreports.engine.JRExporterParameter parameter, Object value)
Convert the supplied parameter value into the actual type required by the correspondingJRExporterParameter.The default implementation simply converts the String values "true" and "false" into corresponding
Booleanobjects, and tries to convert String values that start with a digit intoIntegerobjects (simply keeping them as String if number conversion fails).- 参数:
parameter- the parameter keyvalue- the parameter value- 返回:
- the converted parameter value
getExporterParameter
protected net.sf.jasperreports.engine.JRExporterParameter getExporterParameter(Object parameter)
Return aJRExporterParameterfor the given parameter object, converting it from a String if necessary.- 参数:
parameter- the parameter object, either a String or a JRExporterParameter- 返回:
- a JRExporterParameter for the given parameter object
- 另请参阅:
convertToExporterParameter(String)
convertToExporterParameter
protected net.sf.jasperreports.engine.JRExporterParameter convertToExporterParameter(String fqFieldName)
Convert the given fully qualified field name to a corresponding JRExporterParameter instance.- 参数:
fqFieldName- the fully qualified field name, consisting of the class name followed by a dot followed by the field name (e.g. "net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI")- 返回:
- the corresponding JRExporterParameter instance
loadReport
protected net.sf.jasperreports.engine.JasperReport loadReport()
Load the mainJasperReportfrom the specifiedResource. If theResourcepoints to an uncompiled report design file then the report file is compiled dynamically and loaded into memory.- 返回:
- a
JasperReportinstance, ornullif no main report has been statically defined
loadReport
protected final net.sf.jasperreports.engine.JasperReport loadReport(Resource resource)
Loads aJasperReportfrom the specifiedResource. If theResourcepoints to an uncompiled report design file then the report file is compiled dynamically and loaded into memory.- 参数:
resource- theResourcecontaining the report definition or design- 返回:
- a
JasperReportinstance
renderMergedOutputModel
protected void renderMergedOutputModel(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception
Finds the report data to use for rendering the report and then invokes therenderReport(net.sf.jasperreports.engine.JasperPrint, java.util.Map<java.lang.String, java.lang.Object>, javax.servlet.http.HttpServletResponse)method that should be implemented by the subclass.- 指定者:
renderMergedOutputModel在类中AbstractView- 参数:
model- the model map, as passed in for view rendering. Must contain a report data value that can be converted to aJRDataSource, according to the rules of thefillReport(java.util.Map<java.lang.String, java.lang.Object>)method.request- current HTTP requestresponse- current HTTP response- 抛出:
Exception- if rendering failed
exposeLocalizationContext
protected void exposeLocalizationContext(Map<String,Object> model, HttpServletRequest request)
Expose current Spring-managed Locale and MessageSource to JasperReports i18n ($R expressions etc). The MessageSource should only be exposed as JasperReports resource bundle if no such bundle is defined in the report itself.The default implementation exposes the Spring RequestContext Locale and a MessageSourceResourceBundle adapter for the Spring ApplicationContext, analogous to the
JstlUtils.exposeLocalizationContextmethod.- 另请参阅:
RequestContextUtils.getLocale(javax.servlet.http.HttpServletRequest),MessageSourceResourceBundle,ApplicationObjectSupport.getApplicationContext(),JRParameter.REPORT_LOCALE,JRParameter.REPORT_RESOURCE_BUNDLE,JstlUtils.exposeLocalizationContext(javax.servlet.http.HttpServletRequest, org.springframework.context.MessageSource)
fillReport
protected net.sf.jasperreports.engine.JasperPrint fillReport(Map<String,Object> model) throws Exception
Create a populatedJasperPrintinstance from the configuredJasperReportinstance.By default, this method will use any
JRDataSourceinstance (or wrappableObject) that can be located usingsetReportDataKey(java.lang.String), a lookup for typeJRDataSourcein the model Map, or a special value retrieved viagetReportData(java.util.Map<java.lang.String, java.lang.Object>).If no
JRDataSourcecan be found, this method will use a JDBCConnectionobtained from the configuredjavax.sql.DataSource(or a DataSource attribute in the model). If no JDBC DataSource can be found either, the JasperReports engine will be invoked with plain model Map, assuming that the model contains parameters that identify the source for report data (e.g. Hibernate or JPA queries).- 参数:
model- the model for this request- 返回:
- the populated
JasperPrintinstance - 抛出:
IllegalArgumentException- if noJRDataSourcecan be found and nojavax.sql.DataSourceis suppliedSQLException- if there is an error when populating the report using thejavax.sql.DataSourcenet.sf.jasperreports.engine.JRException- if there is an error when populating the report using aJRDataSourceException- 另请参阅:
getReportData(java.util.Map<java.lang.String, java.lang.Object>),setJdbcDataSource(javax.sql.DataSource)
getReport
protected net.sf.jasperreports.engine.JasperReport getReport()
Determine theJasperReportto fill. Called byfillReport(java.util.Map<java.lang.String, java.lang.Object>).The default implementation returns the report as statically configured through the 'url' property (and loaded by
loadReport()). Can be overridden in subclasses in order to dynamically obtain aJasperReportinstance. As an alternative, consider overriding thefillReport(java.util.Map<java.lang.String, java.lang.Object>)template method itself.- 返回:
- an instance of
JasperReport
getReportData
protected net.sf.jasperreports.engine.JRDataSource getReportData(Map<String,Object> model)
Create an appropriateJRDataSourcefor passed-in report data. Called byfillReport(java.util.Map<java.lang.String, java.lang.Object>)when its own lookup steps were not successful.The default implementation looks for a value of type
java.util.Collectionor object array (in that order). Can be overridden in subclasses.- 参数:
model- the model map, as passed in for view rendering- 返回:
- the
JRDataSourceornullif the data source is not found - 另请参阅:
getReportDataTypes(),convertReportData(java.lang.Object)
convertReportData
protected net.sf.jasperreports.engine.JRDataSource convertReportData(Object value) throws IllegalArgumentException
Convert the given report data value to aJRDataSource.The default implementation delegates to
JasperReportUtilsunless the report data value is an instance ofJRDataSourceProvider. AJRDataSource,JRDataSourceProvider,java.util.Collectionor object array is detected.JRDataSources are returned as is, whilstJRDataSourceProviders are used to create an instance ofJRDataSourcewhich is then returned. The latter two are converted toJRBeanCollectionDataSourceorJRBeanArrayDataSource, respectively.- 参数:
value- the report data value to convert- 返回:
- the JRDataSource
- 抛出:
IllegalArgumentException- if the value could not be converted- 另请参阅:
JasperReportsUtils.convertReportData(java.lang.Object),JRDataSource,JRDataSourceProvider,JRBeanCollectionDataSource,JRBeanArrayDataSource
createReport
protected net.sf.jasperreports.engine.JRDataSource createReport(net.sf.jasperreports.engine.JRDataSourceProvider provider)
Create a report using the given provider.- 参数:
provider- the JRDataSourceProvider to use- 返回:
- the created report
getReportDataTypes
protected Class<?>[] getReportDataTypes()
Return the value types that can be converted to aJRDataSource, in prioritized order. Should only return types that theconvertReportData(java.lang.Object)method is actually able to convert.Default value types are:
java.util.CollectionandObjectarray.- 返回:
- the value types in prioritized order
postProcessReport
protected void postProcessReport(net.sf.jasperreports.engine.JasperPrint populatedReport, Map<String,Object> model) throws Exception
Template method to be overridden for custom post-processing of the populated report. Invoked after filling but before rendering.The default implementation is empty.
- 参数:
populatedReport- the populatedJasperPrintmodel- the map containing report parameters- 抛出:
Exception- if post-processing failed
renderReport
protected abstract void renderReport(net.sf.jasperreports.engine.JasperPrint populatedReport, Map<String,Object> model, HttpServletResponse response) throws Exception
Subclasses should implement this method to perform the actual rendering process.Note that the content type has not been set yet: Implementers should build a content type String and set it via
response.setContentType. If necessary, this can include a charset clause for a specific encoding. The latter will only be necessary for textual output onto a Writer, and only in case of the encoding being specified in the JasperReports exporter parameters.WARNING: Implementers should not use
response.setCharacterEncodingunless they are willing to depend on Servlet API 2.4 or higher. Prefer a concatenated content type String with a charset clause instead.- 参数:
populatedReport- the populatedJasperPrintto rendermodel- the map containing report parametersresponse- the HTTP response the report should be rendered to- 抛出:
Exception- if rendering failed- 另请参阅:
AbstractView.getContentType(),ServletResponse.setContentType(java.lang.String),ServletResponse.setCharacterEncoding(java.lang.String)