📅  最后修改于: 2020-11-16 07:52:51             🧑  作者: Mango
填写报告的主要输入是-报告模板,参数和数据源。本章将描述参数,而在下一章中,我们将讨论数据源。
参数是对象引用,在报表填充操作期间将这些参数传递给报表引擎。不能通过数据源传递的数据可以通过使用参数来传递。可以通过参数传递诸如作者姓名,报告标题等数据。 JasperReports模板或JRXML模板可以具有零个或多个参数元素。
参数声明如下-
class属性也是必需的,它指定参数值的类名称。其默认值为java.lang.String 。可以将其更改为运行时可用的任何类。不管报告参数的类型如何,引擎都会在使用$ P {}标记的报告表达式中进行转换,因此不需要进行手工转换。
报表参数值始终打包在java.util.Map对象中,该对象具有参数名称作为其关键字。可以在报表的查询字符串中使用报表参数,以便进一步自定义从数据库检索的数据集。它们就像在查询中为报表提供数据的动态过滤器一样。
以下是预定义的报告参数,可以在表达式中使用-
S.NO | Parameter Name and Description |
---|---|
1 |
REPORT_PARAMETERS_MAP Contains a map with all user defined and built-in parameters. |
2 |
REPORT_CONNECTION This points to the user supplied class java.sql.Connection, used for JDBC datasources. |
3 |
REPORT_DATA_SOURCE This is a user supplied instance of JRDataSource representing either one of the built-in data source types or a user-defined one. |
4 |
REPORT_MAX_COUNT This is a java.lang.Integer value, allowing the users to limit the records from datasource. |
5 |
REPORT_SCRIPTLET This points to net.sf.jasperreports.engine.JRAbstractScriptlet and contains an instance of the report scriptlet provided by the user. |
6 |
REPORT_LOCALE This a java.util.Locale instance, containing the resource bundle desired locale. |
7 |
REPORT_RESOURCE_BUNDLE This points to java.util.ResourceBundle object and contains localized messages. |
8 |
REPORT_TIME_ZONE This is a java.util.TimeZone instance, used for the date formatting. |
9 |
REPORT_VIRTUALIZER This is an instance of net.sf.jasperreports.engine.JRVirtualizer object, and used for the page virtualization (optimize memory consumption). |
10 |
REPORT_CLASS_LOADER This is a java.lang.ClassLoader instance to be used during the report filling process to load resources such as images, fonts, and subreport templates |
11 |
IS_IGNORE_PAGINATION If set to java.lang.Boolean.TRUE the report will be generated on one long page and page break will not occur. |
让我们将ReportTitle和Author传递给报表(由JasperReportFill.java生成)。修改后的文件C:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ tutorialspoint \ JasperReportFill.java如下-
package com.tutorialspoint;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
public class JasperReportFill {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
String sourceFileName =
"C://tools/jasperreports-5.0.1/test/jasper_report_template.jasper";
DataBeanList DataBeanList = new DataBeanList();
ArrayList dataList = DataBeanList.getDataBeanList();
JRBeanCollectionDataSource beanColDataSource =
new JRBeanCollectionDataSource(dataList);
Map parameters = new HashMap();
/**
* Passing ReportTitle and Author as parameters
*/
parameters.put("ReportTitle", "List of Contacts");
parameters.put("Author", "Prepared By Manisha");
try {
JasperFillManager.fillReportToFile(
sourceFileName, parameters, beanColDataSource);
} catch (JRException e) {
e.printStackTrace();
}
}
}
POJO文件C:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ tutorialspoint \ DataBean.java的内容如下-
package com.tutorialspoint;
public class DataBean {
private String name;
private String country;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
文件C:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ tutorialspoint \ DataBeanList.java的内容如下所示-
package com.tutorialspoint;
import java.util.ArrayList;
public class DataBeanList {
public ArrayList getDataBeanList() {
ArrayList dataBeanList = new ArrayList();
dataBeanList.add(produce("Manisha", "India"));
dataBeanList.add(produce("Dennis Ritchie", "USA"));
dataBeanList.add(produce("V.Anand", "India"));
dataBeanList.add(produce("Shrinath", "California"));
return dataBeanList;
}
/**
* This method returns a DataBean object,
* with name and country set in it.
*/
private DataBean produce(String name, String country) {
DataBean dataBean = new DataBean();
dataBean.setName(name);
dataBean.setCountry(country);
return dataBean;
}
}
让我们将参数< ReportTitle >和< Author >添加到我们现有的报告模板(Chapter Report Designs )。报告标题和作者将显示在报告的开头。修改后的报告模板(jasper_report_template.jrxml)如下。将其保存到C:\ tools \ jasperreports-5.0.1 \ test目录-
我们将使用常规的ANT构建过程来编译并执行上述文件。文件build.xml的内容(保存在目录C:\ tools \ jasperreports-5.0.1 \ test下)如下。
导入文件-baseBuild.xml是从“环境设置”一章中选取的,应与build.xml放在同一目录中。
接下来,让我们打开命令行窗口并转到build.xml所在的目录。最后,执行以下命令ant -Dmain-class = com.tutorialspoint.JasperReportFill (viewFullReport是默认目标)-
C:\tools\jasperreports-5.0.1\test>ant -Dmain-class=com.tutorialspoint.JasperReportFill
Buildfile: C:\tools\jasperreports-5.0.1\test\build.xml
clean-sample:
[delete] Deleting directory C:\tools\jasperreports-5.0.1\test\classes
[delete] Deleting: C:\tools\jasperreports-5.0.1\test\jasper_report_template.jasper
[delete] Deleting: C:\tools\jasperreports-5.0.1\test\jasper_report_template.jrprint
compile:
[mkdir] Created dir: C:\tools\jasperreports-5.0.1\test\classes
[javac] C:\tools\jasperreports-5.0.1\test\baseBuild.xml:28: warning:
'includeantruntime' was not set, defaulting to build.sysclasspath=last;
set to false for repeatable builds
[javac] Compiling 7 source files to C:\tools\jasperreports-5.0.1\test\classes
compilereportdesing:
[jrc] Compiling 1 report design files.
[jrc] log4j:WARN No appenders could be found for logger
(net.sf.jasperreports.engine.xml.JRXmlDigesterFactory).
[jrc] log4j:WARN Please initialize the log4j system properly.
[jrc] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig
for more info.
[jrc] File : C:\tools\jasperreports-5.0.1\test\jasper_report_template.jrxml ... OK.
run:
[echo] Runnin class : com.tutorialspoint.JasperReportFill
[java] log4j:WARN No appenders could be found for logger
(net.sf.jasperreports.extensions.ExtensionsEnvironment).
[java] log4j:WARN Please initialize the log4j system properly.
viewFillReport:
[java] log4j:WARN No appenders could be found for logger
(net.sf.jasperreports.extensions.ExtensionsEnvironment).
[java] log4j:WARN Please initialize the log4j system properly.
BUILD SUCCESSFUL
Total time: 18 seconds
经过以上编译,JasperViewer窗口打开,如下图所示:
在这里,我们看到,ReportTitle“联系人列表”和作者“ Manisha准备”显示在报告的开头。