Struts2.1.8+JasperReport3.7.2结合iReport3.7.2报表开发

一、准备
下载iReport3.7.1、JasperReport3.7.1、Struts2.1.8及其他需要的包
如需生成Excel的话,还需导入poi-3.5.jar包。

二、整合Struts2+JasperReport3
1. 新建以下三个pojo类:
Address

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package com.xy.report.pojo;

public class Address{
private Stringcity;
private Stringzipcode;
public StringgetCity(){
return city;
}
public void setCity(Stringcity){
this .city = city;
}
public StringgetZipcode(){
return zipcode;
}
public void setZipcode(Stringzipcode){
this .zipcode = zipcode;
}
}

Company

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package com.xy.report.pojo;

import java.util.HashSet;
import java.util.Set;

public class Company implements Comparable < Company > {
private Stringname;
private Addressaddress;
private Set < Staff > staffs = new HashSet < Staff > ();

@Override
public int compareTo(Companyo){
return o.getName().compareTo( this .getName());
}

public StringgetName(){
return name;
}
public void setName(Stringname){
this .name = name;
}
public AddressgetAddress(){
return address;
}
public void setAddress(Addressaddress){
this .address = address;
}
public Set < Staff > getStaffs(){
return staffs;
}
public void setStaffs(Set < Staff > staffs){
this .staffs = staffs;
}

}

Company中compareTo方法用于在报表中排序。
Staff

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package com.xy.report.pojo;

public class Staff{
private Stringname;
private Companycompany;

public StringgetName(){
return name;
}

public void setName(Stringname){
this .name = name;
}

public CompanygetCompany(){
return company;
}

public void setCompany(Companycompany){
this .company = company;
}


}

以上Address是Company的组件,Company与Staff是一对多关系。
2. 新建Action

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package com.xy.report.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.xy.report.pojo.Address;
import com.xy.report.pojo.Company;
import com.xy.report.pojo.Staff;

public class DemoAction extends ActionSupport{

private static final long serialVersionUID = - 5460323122712890562L ;

private List < Staff > list;
private Map < String,Object > map;

@Override
public Stringexecute(){
list
= getData();
map
= getParameter();

return SUCCESS;
}

private List < Staff > getData(){
Addressaddress
= new Address();
address.setCity(
" 上海 " );
address.setZipcode(
" 201400 " );

Companycompany1
= new Company();
company1.setName(
" 飞利浦 " );
company1.setAddress(address);
Companycompany2
= new Company();
company2.setName(
" 谷歌 " );
company2.setAddress(address);

Staffstaff1
= new Staff();
staff1.setName(
" 张三 " );
staff1.setCompany(company1);
Staffstaff2
= new Staff();
staff2.setName(
" 李四 " );
staff2.setCompany(company2);
Staffstaff3
= new Staff();
staff3.setName(
" 王五 " );
staff3.setCompany(company1);

List
< Staff > list = new ArrayList < Staff > ();
list.add(staff1);
list.add(staff2);
list.add(staff3);

return list;
}

private Map < String,Object > getParameter(){
Map
< String,Object > map = new HashMap < String,Object > ();
map.put(
" date " , " 2010-03-12 " );
return map;
}

// getter,setter
public List < Staff > getList(){
return list;
}
public void setList(List < Staff > list){
this .list = list;
}
public Map < String,Object > getMap(){
return map;
}
public void setMap(Map < String,Object > map){
this .map = map;
}
}

以上list为提供给报表的数据源,map为提供给报表的参数,如日期范围等。struts.xml配置如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <? xmlversion="1.0"encoding="UTF-8" ?>
<! DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< include file ="struts-default.xml" ></ include >

<!-- 报表演示 -->
< package name ="default" namespace ="/reports" extends ="struts-default,jasperreports-default" >
< action name ="demoReport" class ="com.xy.report.action.DemoAction" >
< result name ="success" type ="jasper" >
< param name ="location" > /reports/demoReport.jasper </ param >
< param name ="dataSource" > list </ param >
< param name ="format" > PDF </ param >
< param name ="reportParameters" > map </ param >
</ result >
</ action >
</ package >
</ struts >

location - 提供利用iReport制作编译好的报表文件。
dataSource - 数据源
format - 报表输出格式
reportParameters - 报表参数

三、利用iReport开发报表
1. 新建数据源
打开iReport,点击下图像插头一样的Report Datasources,弹出对象框中New,选择"JavaBeans set datasource"后Next,输入Name为"demo"后Save,如下两图:

你可能感兴趣的:(apache,xml,struts,Excel)