暑期项目经验(六)--struts+jasperreporters
struts + jasperreporters
1.jasperreporters+ireport,版本要对应,先用ireport制作相应模版,生成相应.jasper文件。
实例如下:
2.jar包:
3.实体类
package com.pxj.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; } } package com.pxj.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>(); 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; } public int compareTo(Company o) { return o.getName().compareTo(this.getName()); } } package com.pxj.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; } }
4.Action
package com.pxj.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.pxj.report.pojo.Address; import com.pxj.report.pojo.Company; import com.pxj.report.pojo.Staff; public class DemoAction extends ActionSupport{ private static final long serialVersionUID = 1446363L; private List<Staff> list; Map<String,Object> map; 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; } @Override public String execute() throws Exception { list = getData(); map = getParameter(); return super.execute(); } private Map<String, Object> getParameter() { Map<String,Object> map = new HashMap<String,Object>(); map.put("date", "2014-08-04"); return map; } private List<Staff> getData() { Address addr = new Address(); addr.setCity("重庆"); addr.setZipcode("20140804"); Company company1 = new Company(); company1.setName("天友"); company1.setAddress(addr); Company company2 = new Company(); company2.setName("谷歌"); company2.setAddress(addr); 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; } }
5.struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="project" namespace="/" extends="jasperreports-default"> <action name="viewDemo" class="com.pxj.report.action.DemoAction"> <result name="success" type="jasper"> <!-- 指定类型 --> <param name="location">/jasper/strust2Demo.jasper</param> <!-- 指定jasper文件位置 --> <param name="dataSource">list</param> <!-- 指定数据源 --> <param name="format">PDF</param> <!-- 指定生成格式 --> <param name="reportParameters">map</param> <!-- 指定传递替换参数 --> </result> </action> </package> </struts>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。