create Bean Data Sources

Jasper report创建bean数据源的方法

The bean data source type is a key extension because it allows you to make use of any custom or exotic data that you might need to report on. Bean data sources serve as a bridge between a Spring-defined bean and a JasperReport. The Spring bean is responsible for providing the data or parameters that fill the report.

To use a bean data source, you must first configure the underlying Spring bean and make it available in the server’s web application context. For example, you would add a bean definition to one of the WEB-INF/applicationContext*.xml files.

The bean must resolve to a ReportDataSourceService instance, either directly or by way of a factory no-argument method. You can use any Spring instantiation method (for example, a constructor or factory) and bean scope (for example, singleton or prototype) for the data source service bean.

The ReportDataSourceService instance is responsible for supplying data source parameters to the JasperReport. Custom ReportDataSourceServiceimplementations can follow two approaches:

  If the implementation can provide the data to be used to fill a report, it needs to wrap the data into a suitable JRDataSource implementation and pass the data using the REPORT_DATA_SOURCE report parameter.
  If the data comes from the report query by way of a JasperReports query executor, the data source service must set values for the connection parameters defined by the query executor. The connection parameters are usually obtained from the properties of the data source service instance.

For example, you could implement a Hibernate data source service that would be injected in a session factory. The factory would create a Hibernate session that would be passed as a value for the HIBERNATE_SESSION parameter. The JasperReports Hibernate query executor then uses the parameter to run the HQL report query.

The ReportDataSourceService interface contains two methods: setReportParameterValues and closeConnection. The former provides data and connection parameter values; the latter is required to close and release any resources or connections created during the call to setReportParameterValues.

Once the data source service bean is available through Spring, you can add the bean data source to the repository.

To create a bean data source:

  1. Log on as an administrator.
  2. Click View > Repository, expand the folder tree, and right-click a folder to select Add Resource > Data Source from the context menu. Alternatively, you can select Create > Data Source from the main menu on any page and specify a folder location later. If you installed the sample data, the suggested folder is Data Sources.

The Add Data Source page appears, as shown in Bean Data Source Page.

  3. In the Type field, select Bean Data Source.

The information on the page changes to reflect what’s needed to define a bean data source.

  4. Fill in the required fields, along with any optional information.

If the data source service is to be instantiated through a factory method of the Spring bean, you should also enter the name of the method.

  5. Click Test Connection to validate the data source.

If the validation fails, ensure that the values you entered are correct and that the bean is in the classpath.

  6. When the test is successful, click Save.
上面的是在ireport安装部署的过程,下面是代码

代码:

实体Bean,用来展示字段

package com.isprint.report;

public class ReportOneBean {

	private String name;	//申请人名称
	private String userId;	//申请人ID
	private String organization; //人员组织机构
	private String bizRoleCode;  //岗位ID
	private String bizRoleName;  //岗位名称
	private String resource;   //业务系统
	private String itRoleCode;  //授权角色id
	private String itRoleName;  //授权角色
	private String orgScope;      //机构维度
	private String productScope;  //产品维度
	private String resourceScope; //系统维度
	private String zhangTaoScope; //账户维度
	private String approveName;   //审批人
	private String approveReason; //审批意见
	private String createTime;	//申请时间
	private String processId;	//任务编码
	
	
	public ReportOneBean(String name, String userId, String organization,
			String bizRoleCode, String bizRoleName, String resource,
			String itRoleCode, String itRoleName, String orgScope,
			String productScope, String resourceScope, String zhangTaoScope,
			String approveName, String approveReason, String createTime,
			String processId) {
		this.name = name;
		this.userId = userId;
		this.organization = organization;
		this.bizRoleCode = bizRoleCode;
		this.bizRoleName = bizRoleName;
		this.resource = resource;
		this.itRoleCode = itRoleCode;
		this.itRoleName = itRoleName;
		this.orgScope = orgScope;
		this.productScope = productScope;
		this.resourceScope = resourceScope;
		this.zhangTaoScope = zhangTaoScope;
		this.approveName = approveName;
		this.approveReason = approveReason;
		this.createTime = createTime;
		this.processId = processId;
	}
	 
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getOrganization() {
		return organization;
	}
	public void setOrganization(String organization) {
		this.organization = organization;
	}
	public String getBizRoleCode() {
		return bizRoleCode;
	}
	public void setBizRoleCode(String bizRoleCode) {
		this.bizRoleCode = bizRoleCode;
	}
	public String getBizRoleName() {
		return bizRoleName;
	}
	public void setBizRoleName(String bizRoleName) {
		this.bizRoleName = bizRoleName;
	}
	public String getResource() {
		return resource;
	}
	public void setResource(String resource) {
		this.resource = resource;
	}
	public String getItRoleCode() {
		return itRoleCode;
	}
	public void setItRoleCode(String itRoleCode) {
		this.itRoleCode = itRoleCode;
	}
	public String getItRoleName() {
		return itRoleName;
	}
	public void setItRoleName(String itRoleName) {
		this.itRoleName = itRoleName;
	}
	public String getOrgScope() {
		return orgScope;
	}
	public void setOrgScope(String orgScope) {
		this.orgScope = orgScope;
	}
	public String getProductScope() {
		return productScope;
	}
	public void setProductScope(String productScope) {
		this.productScope = productScope;
	}
	public String getResourceScope() {
		return resourceScope;
	}
	public void setResourceScope(String resourceScope) {
		this.resourceScope = resourceScope;
	}
	public String getZhangTaoScope() {
		return zhangTaoScope;
	}
	public void setZhangTaoScope(String zhangTaoScope) {
		this.zhangTaoScope = zhangTaoScope;
	}
	public String getApproveName() {
		return approveName;
	}
	public void setApproveName(String approveName) {
		this.approveName = approveName;
	}
	public String getApproveReason() {
		return approveReason;
	}
	public void setApproveReason(String approveReason) {
		this.approveReason = approveReason;
	}
	public String getCreateTime() {
		return createTime;
	}
	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}
	public String getProcessId() {
		return processId;
	}
	public void setProcessId(String processId) {
		this.processId = processId;
	}
	
	
}

ReportCustomDataSource 继承JRDataSource,显示数据

package com.isprint.report;
import java.text.SimpleDateFormat;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

public class ReportCustomDataSource implements JRDataSource
{
	private Object[][] data =
		{
			{"Berne","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa",new Long(1), "adfasdfasfasd"},
			{"Berne2","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa",new Long(2), "adfasdfasfasd"},
			{"Berne3","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa",new Long(3), "adfasdfasfasd"},
			{"Berne4","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa",new Long(4), "adfasdfasfasd"},
			{"Berne5","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa",new Long(5), "adfasdfasfasd"},
			{"Berne6","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa",new Long(6), "adfasdfasfasd"},
			{"Berne7","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa",new Long(7), "aaaaaaa"}
		};

	private int index = -1;
	public ReportCustomDataSource()
	{
	}
	public boolean next() throws JRException
	{
		index++;
		return (index < data.length);
	}

	public Object getFieldValue(JRField field) throws JRException
	{
		SimpleDateFormat sdf = new SimpleDateFormat();
		Object value = null;
		String fieldName = field.getName();
		if ("name".equals(fieldName)) {
			value = data[index][0];
		} else if ("organization".equals(fieldName)) {
			value = data[index][1];
		}else if ("bizRoleCode".equals(fieldName)) {
			value = data[index][2];
		}else if ("bizRoleName".equals(fieldName)) {
			value = data[index][3];
		}else if ("itRoleCode".equals(fieldName)) {
			value = data[index][4];
		}else if ("itRoleName".equals(fieldName)) {
			value = data[index][5];
		}else if ("orgScope".equals(fieldName)) {
			value = data[index][6];
		}else if ("productScope".equals(fieldName)) {
			value = data[index][7];
		}else if ("resourceScope".equals(fieldName)) {
			value = data[index][8];
		}else if ("zhangTaoScope".equals(fieldName)) {
			value = data[index][9];
		}else if ("approveName".equals(fieldName)) {
			value = data[index][10];
		}else if ("approveReason".equals(fieldName)) {
			value = data[index][11];
		}else if ("createTime".equals(fieldName)) {
			try {
				value = sdf.parse(String.valueOf(data[index][12]));
			} catch (Exception pe) {
				// do nothing
			} finally {
			}
		} else if ("processId".equals(fieldName)) {
			value = data[index][13];
		}
		return value;
	}
}

ReportCustomBeanFactory的getBeanCollection用来调用数据。

package com.isprint.report;

import java.util.Arrays;
import java.util.Collection;

public class ReportCustomBeanFactory
{
	private static ReportOneBean[] data =
		{
			new ReportOneBean("Berne","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa","2015-4-2", "277 Seventh Av."),
			new ReportOneBean("Berne2","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa","2015-4-2", "277 Seventh Av."),
			new ReportOneBean("Berne3","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa","2015-4-2", "277 Seventh Av."),
			new ReportOneBean("Berne4","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa","2015-4-2", "277 Seventh Av."),
			new ReportOneBean("Berne5","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa","2015-4-2", "277 Seventh Av."),
			new ReportOneBean("Berne6","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa","2015-4-2", "277 Seventh Av."),
			new ReportOneBean("Berne7","1", "信科","123","经理","aa","Berne","aa","Berne","aa","Berne","aa","Berne","aa","2015-4-2", "277 Seventh Av.")
			
		};  
	public static Object[] getBeanArray()
	{
		return data;
	}
	public static Collection getBeanCollection()
	{
		return Arrays.asList(data);
	}


}

bean数据源可以用来设计报表模板,在ireport里面引入代码的jar报或者class文件,然后经过配置就可以看到代码里的写的数据。


你可能感兴趣的:(create Bean Data Sources)