---
title: "关于jasperReport 和Struts2 整合的一些问题"
author: "BestUpon"
date: "2009-10-16T14:28:43Z"
tags: []
categories: ["编程语言"]
slug: "jasperreport-struts2"
draft: false
lastmod: "2010-06-12T01:53:57Z"
id: 492245
url: "https://bestupon.iteye.com/blog/492245"
---

找了找网上原先的例子，没有一个适合我的，所以就摸着石头过河，最主要的还是要找对jar包，最近新出的开发工具是ireport3.6.0界面友好、方便使用，但是其包很难找对，一下就是Jar包。![见下图jar](http://dl.iteye.com/upload/attachment/157157/dd49d613-0e11-3c31-b502-42deb6bc6644.png)

 

但是和Spring一起整合开发的时候会一直抛出一个异常struts-default找不见这个异常出现，解决办法就是将原先使用的xercesImpl-2.6.2.jar 给删除掉。

使用iReport绘制好一个报表的模板，在请求的Action的方法中将其编译好，并设置好数据源。

```
	try {
			String reportSource;
			reportSource = ServletActionContext.getServletContext()
					.getRealPath("/jasper/jasper.jrxml");
			File parent = new File(reportSource).getParentFile();
			//将.jrxml模板文件编译成为.jasper文件,当然,其文件名可以指定,如果没指定,则与.jrxml文件名一样.只是后缀不同而已
			JasperCompileManager.compileReportToFile(reportSource, new File(
					parent, "jasper.jasper")
					.getAbsolutePath());
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
```

 

在页面端直接访问就可以得到PDF，HTML、XML等等数据。（中文问题在前面的文章中解决过了）参见：[http://bestupon.iteye.com/blog/488426](/blog/488426)