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

package  com.xy.report.pojo;

public   class  Address {
    
private  String city;
    
private  String zipcode;
    
public  String getCity() {
        
return  city;
    }
    
public   void  setCity(String city) {
        
this .city  =  city;
    }
    
public  String getZipcode() {
        
return  zipcode;
    }
    
public   void  setZipcode(String zipcode) {
        
this .zipcode  =  zipcode;
    }
}

Company

package  com.xy.report.pojo;

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

public   class  Company  implements  Comparable < Company > {
    
private  String name;
    
private  Address address;
    
private  Set < Staff >  staffs  =   new  HashSet < Staff > ();
    
    @Override
    
public   int  compareTo(Company o) {
        
return  o.getName().compareTo( this .getName());
    }

    
public  String getName() {
        
return  name;
    }
    
public   void  setName(String name) {
        
this .name  =  name;
    }
    
public  Address getAddress() {
        
return  address;
    }
    
public   void  setAddress(Address address) {
        
this .address  =  address;
    }
    
public  Set < Staff >  getStaffs() {
        
return  staffs;
    }
    
public   void  setStaffs(Set < Staff >  staffs) {
        
this .staffs  =  staffs;
    }

}

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

package  com.xy.report.pojo;

public   class  Staff {
    
private  String name;
    
private  Company company;

    
public  String getName() {
        
return  name;
    }

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

    
public  Company getCompany() {
        
return  company;
    }

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

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

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  String execute(){
        list 
=  getData();
        map 
=  getParameter();
        
        
return  SUCCESS;
    }
    
    
private  List < Staff >  getData(){
        Address address 
=   new  Address();
        address.setCity(
" 上海 " );
        address.setZipcode(
" 201400 " );

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

        Staff staff1 
=   new  Staff();
        staff1.setName(
" 张三 " );
        staff1.setCompany(company1);
        Staff staff2 
=   new  Staff();
        staff2.setName(
" 李四 " );
        staff2.setCompany(company2);
        Staff staff3 
=   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配置如下:

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.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,如下两图:

 

你可能感兴趣的:(struts,list,report,报表,encoding,javabeans)