您的位置:首页 > 编程语言 > ASP

JasperReport与Struts2整合开发报表

2011-02-13 22:11 330 查看
GPL和LGPL

GPL协议和LGPL协议的工具或者框架的本身都是开源的
但在使用GPL协议的工具或者框架时,就要求你自己的代码也必须开源
使用LGPL协议的工具或者框架时,则不要求开源你自己的源代码
所以商业项目中通常会使用LGPL的工具,而一般很少使用GPL的工具

JasperReport与Struts2整合
Struts2整合JFreeChart时,图片是动态生成的,只需在程序中描述好图片属性即可
但对于JasperReport,首先要设计好报表,然后在Action中提供报表所需数据
并作为数据源赋给生成的jasper文件,最后令其以指定的格式呈现出来就可以了

这是使用JasperReport与Struts2.0.11集成开发报表的demo工程

首先是web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>


然后是index.jsp页面

<%@ page pageEncoding="UTF-8"%>
<%response.sendRedirect(request.getContextPath() + "/viewJasperReport.action"); %>


接着是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>
	<!-- Struts2的包支持多继承 -->
	<!-- 不要忘记引入struts2-jasperreports-plugin-2.0.11.jar以及jasperreports-3.7.1-project.zip的lib下的所有Jar包 -->
	<package name="struts2" extends="struts-default, jasperreports-default">
		<action name="viewJasperReport" class="com.jadyer.action.JasperReportAction">
			<result name="success" type="jasper">
				<param name="location">test.jasper</param>
				<param name="dataSource">list</param>
				<param name="format">HTML</param>
				<!-- <param name="format"></param>参数format的缺省值为PDF -->
				<!-- 参数location用于指定通过iReport设计好的报表文件的位置 -->
				<!-- 在物理路径上,location是相对于项目的WebRoot的 -->
				<!-- 从URL上来说,location是相对于http://localhost:8088/当前项目名/的 -->
			</result>
		</action>
	</package>
</struts>


然后是用到的实体类

package com.jadyer.model;

public class Person {
	private int id;
	private String name;
	private int age;
	private String address;
	
	/* 四个属性对应的setter和getter略 */
	
	public Person() {}

	public Person(int id, String name, int age, String address) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.address = address;
	}
}


最后是为JasperReport提供数据源的Action类

package com.jadyer.action;

import java.util.LinkedList;
import java.util.List;

import com.jadyer.bean.Person;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class JasperReportAction extends ActionSupport {
	//其实报表的数据,基本上都是重复的,相当于一个集合,报表的每一行就是集合里的某一个元素
	//这里为了简化,就不使用数据库了。其实是否使用数据库,对应我们前端整合没有任何影响
	//而在真正的项目开发时,很少把来自于数据库中的数据,直接赋到报表上
	//一般都是从数据库获取数据,然后按照用户的需求经过一定处理,再把它赋给报表
	private List<Person> list;

	//该方法返回一个List,它返回的数据是给报表使用的,那么它就是报表的数据源
	//所以在struts.xml中设置为<param name="dataSource">list</param>
	//此时jasperreport就会通过反射自动到这里查找名为getList()的方法
	//然后就会得到一个返回值,供报表使用
	public List<Person> getList() {
		this.list = new LinkedList<Person>();
		this.list.add(new Person(1, "张起灵", 20, "七星鲁王"));
		this.list.add(new Person(2, "陈文锦", 30, "蛇沼鬼城"));
		this.list.add(new Person(3, "吴三省", 40, "怒海潜沙"));
		this.list.add(new Person(4, "四阿公", 50, "云顶天宫"));		
		return this.list;
	}

	public void setList(List<Person> list) {
		this.list = list;
	}
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
}


附:该工程需要用到iReport开发设计出来的test.jasper文件

附:这是test.jasper的下载地址----下载地址一 下载地址二
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: