📄 abstractjasperreportsview.java
字号:
* @see #convertReportData
* @see net.sf.jasperreports.engine.JRDataSource
* @see net.sf.jasperreports.engine.data.JRBeanCollectionDataSource
* @see net.sf.jasperreports.engine.data.JRBeanArrayDataSource
*/
public void setSubReportDataKeys(String[] subReportDataKeys) {
this.subReportDataKeys = subReportDataKeys;
}
/**
* Specify the set of headers that are included in each of response.
* @param headers the headers to write to each response.
*/
public void setHeaders(Properties headers) {
this.headers = headers;
}
/**
* Set the exporter parameters that should be used when rendering a view.
* @param parameters <code>Map</code> with the fully qualified field name
* of the <code>JRExporterParameter</code> instance as key
* (e.g. "net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI")
* and the value you wish to assign to the parameter as value
*/
public void setExporterParameters(Map parameters) {
// NOTE: Removed conversion from here since configuration of parameters
// can also happen through access to the underlying Map using
// getExporterParameters(). Conversion now happens in initApplicationContext,
// and subclasses use getConvertedExporterParameters() to access the converted
// parameter Map - robh.
this.exporterParameters = parameters;
}
/**
* Return the exporter parameters that this view uses, if any.
*/
public Map getExporterParameters() {
return this.exporterParameters;
}
/**
* Allows subclasses to retrieve the converted exporter parameters.
*/
protected Map getConvertedExporterParameters() {
return this.convertedExporterParameters;
}
/**
* Specify the <code>javax.sql.DataSource</code> to use for reports with
* embedded SQL statements.
*/
public void setJdbcDataSource(DataSource jdbcDataSource) {
this.jdbcDataSource = jdbcDataSource;
}
/**
* Return the <code>javax.sql.DataSource</code> that this view uses, if any.
*/
protected DataSource getJdbcDataSource() {
return jdbcDataSource;
}
/**
* Specify the JRCompiler implementation to use for compiling a ".jrxml"
* report file on-the-fly into a report class.
* <p>By default, a JRDefaultCompiler will be used, delegating to the
* Eclipse JDT compiler or the Sun JDK compiler underneath.
* @see net.sf.jasperreports.engine.design.JRDefaultCompiler
*/
public void setReportCompiler(JRCompiler reportCompiler) {
this.reportCompiler = reportCompiler;
}
/**
* Return the JRCompiler instance to use for compiling ".jrxml" report files.
*/
protected JRCompiler getReportCompiler() {
if (this.reportCompiler == null) {
// No JR 1.0.1 JRDefaultCompiler.getInstance() method available -
// assuming public JRDefaultCompiler no-arg constructor (<= JR 1.0.0)
this.reportCompiler = (JRCompiler) BeanUtils.instantiateClass(JRDefaultCompiler.class);
}
return reportCompiler;
}
/**
* Checks to see that a valid report file URL is supplied in the
* configuration. Compiles the report file is necessary.
*/
protected void initApplicationContext() throws ApplicationContextException {
Resource mainReport = getApplicationContext().getResource(getUrl());
this.report = loadReport(mainReport);
// Load sub reports if required, and check data source parameters.
if (this.subReportUrls != null) {
if (this.subReportDataKeys != null && this.subReportDataKeys.length > 0 && this.reportDataKey == null) {
throw new ApplicationContextException(
"'reportDataKey' for main report is required when specifying a value for 'subReportDataKeys'");
}
this.subReports = new HashMap(this.subReportUrls.size());
for (Enumeration urls = this.subReportUrls.propertyNames(); urls.hasMoreElements();) {
String key = (String) urls.nextElement();
String path = this.subReportUrls.getProperty(key);
Resource resource = getApplicationContext().getResource(path);
this.subReports.put(key, loadReport(resource));
}
}
// Convert user-supplied exporterParameters.
convertExporterParameters();
if (this.headers == null) {
this.headers = new Properties();
}
if (!this.headers.containsKey(HEADER_CONTENT_DISPOSITION)) {
this.headers.setProperty(HEADER_CONTENT_DISPOSITION, CONTENT_DISPOSITION_INLINE);
}
}
/**
* Converts the exporter parameters passed in by the user which may be keyed
* by <code>String</code>s corresponding to the fully qualified name of the
* <code>JRExporterParameter</code> into parameters which are keyed by
* <code>JRExporterParameter</code>.
* @see #getExporterParameter(Object)
*/
protected final void convertExporterParameters() {
if (this.exporterParameters != null && !this.exporterParameters.isEmpty()) {
this.convertedExporterParameters = new HashMap(this.exporterParameters.size());
for (Iterator it = this.exporterParameters.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
this.convertedExporterParameters.put(getExporterParameter(entry.getKey()), entry.getValue());
}
}
}
/**
* Return a <code>JRExporterParameter</code> for the given parameter object,
* converting it from a String if necessary.
* @param parameter the parameter object, either a String or a JRExporterParameter
* @return a JRExporterParameter for the given parameter object
* @see #convertToExporterParameter(String)
*/
protected JRExporterParameter getExporterParameter(Object parameter) {
if (parameter instanceof JRExporterParameter) {
return (JRExporterParameter) parameter;
}
if (parameter instanceof String) {
return convertToExporterParameter((String) parameter);
}
throw new IllegalArgumentException(
"Parameter [" + parameter + "] is invalid type. Should be either String or JRExporterParameter.");
}
/**
* Convert the given fully qualified field name to a corresponding
* JRExporterParameter instance.
* @param 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")
* @return the corresponding JRExporterParameter instance
*/
protected JRExporterParameter convertToExporterParameter(String fqFieldName) {
int index = fqFieldName.lastIndexOf('.');
if (index == -1 || index == fqFieldName.length()) {
throw new IllegalArgumentException(
"Parameter name [" + fqFieldName + "] is not a valid static field. " +
"The parameter name must map to a static field such as " +
"[net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI]");
}
String className = fqFieldName.substring(0, index);
String fieldName = fqFieldName.substring(index + 1);
try {
Class cls = ClassUtils.forName(className);
Field field = cls.getField(fieldName);
if (JRExporterParameter.class.isAssignableFrom(field.getType())) {
try {
return (JRExporterParameter) field.get(null);
}
catch (IllegalAccessException ex) {
throw new IllegalArgumentException(
"Unable to access field [" + fieldName + "] of class [" + className + "]. " +
"Check that it is static and accessible.");
}
}
else {
throw new IllegalArgumentException("Field [" + fieldName + "] on class [" + className +
"] is not assignable from JRExporterParameter - check the type of this field.");
}
}
catch (ClassNotFoundException ex) {
throw new IllegalArgumentException(
"Class [" + className + "] in key [" + fqFieldName + "] could not be found.");
}
catch (NoSuchFieldException ex) {
throw new IllegalArgumentException("Field [" + fieldName + "] in key [" + fqFieldName +
"] could not be found on class [" + className + "].");
}
}
/**
* Loads a <code>JasperReport</code> from the specified <code>Resource</code>. If
* the <code>Resource</code> points to an uncompiled report design file then the
* report file is compiled dynamically and loaded into memory.
* @param resource the <code>Resource</code> containing the report definition or design
* @return a <code>JasperReport</code> instance
*/
private JasperReport loadReport(Resource resource) throws ApplicationContextException {
try {
String fileName = resource.getFilename();
if (fileName.endsWith(".jasper")) {
// Load pre-compiled report.
if (logger.isInfoEnabled()) {
logger.info("Loading pre-compiled Jasper Report from " + resource);
}
return (JasperReport) JRLoader.loadObject(resource.getInputStream());
}
else if (fileName.endsWith(".jrxml")) {
// Compile report on-the-fly.
if (logger.isInfoEnabled()) {
logger.info("Compiling Jasper Report loaded from " + resource);
}
JasperDesign design = JRXmlLoader.load(resource.getInputStream());
return getReportCompiler().compileReport(design);
}
else {
throw new IllegalArgumentException(
"Report URL [" + getUrl() + "] must end in either .jasper or .jrxml");
}
}
catch (IOException ex) {
throw new ApplicationContextException(
"Could not load JasperReports report for URL [" + getUrl() + "]", ex);
}
catch (JRException ex) {
throw new ApplicationContextException(
"Could not parse JasperReports report for URL [" + getUrl() + "]", ex);
}
}
/**
* Finds the report data to use for rendering the report and then invokes the
* <code>renderReport</code> method that should be implemented by the subclass.
* @param model the model map, as passed in for view rendering. Must contain
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -