Spring3 Web MVC 集成Jasper Report生成PDF例子

Spring3 Web MVC 集成JasperReport生成PDF例子

一:环境搭建与配置

1.      安装JDK6以上版本

2.      安装STS, 下载地址如下:http://www.springsource.org/downloads/sts-ggts

3.      下载并安装Tomcat7

4.      创建一个Dynamic Web Project项目,然后选择创建好的项目右键选择

Configuration->convert to Manve Project.

5.      添加web.xml文件,在WEB-INF目录下新建reports, pages, classes三个子目录

6.      新建index.jsp文件在webapp目录下。

7.      最终的项目目录结构如下:


二:Spring配置详解

在web.xml中配置Spring的DispatchServlet与Context Listener,配置如下:


在express-servlet.xml中配置spring view解析器


三:Jasper Report配置详解

在jasper-views.xml添加如下配置


四:Report内容与数据源

两个报表,演示了子报表的用法,同时还演示了如何想子报表传递数据源,以及参

数传递在报表中显示图像等技巧。需要特别说明的是如果要在报表中使用图像路径

图像必须位于WEB-INF/classes下面,因为JasperReport解析是按找类路径寻找。关

于报表的详细内容建议查看下载以后的源文件,此处不再细说。

五:Controller与注解

Spring3 Controller支持注解(annotation)方式,使用非常方便,生成PDF报表的

Controller代码如下:

[java]  view plain copy
  1. package com.gloomyfish.express.controllers;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import net.sf.jasperreports.engine.JRDataSource;  
  7. import net.sf.jasperreports.engine.JREmptyDataSource;  
  8.   
  9. import org.apache.log4j.Logger;  
  10. import org.springframework.stereotype.Controller;  
  11. import org.springframework.web.bind.annotation.RequestMapping;  
  12. import org.springframework.web.bind.annotation.RequestMethod;  
  13. import org.springframework.web.servlet.ModelAndView;  
  14.   
  15. import com.gloomyfish.report.dao.MockDataFactory;  
  16.   
  17. @Controller  
  18. public class JasperReportController {  
  19.       
  20.     protected static Logger logger = Logger.getLogger("controller");  
  21.       
  22.     /** 
  23.      * Retrieves the PDF report file 
  24.      *  
  25.      * @return 
  26.      */  
  27.     @RequestMapping(value = "/getpdfReport", method = RequestMethod.GET)  
  28.     public ModelAndView doSalesReportPDF(ModelAndView modelAndView)   
  29.          {  
  30.         logger.debug("Received request to download PDF report");  
  31.           
  32.         // Retrieve our data from a mock data provider  
  33.         MockDataFactory dataprovider = new MockDataFactory();  
  34.           
  35.         // Assign the datasource to an instance of JRDataSource  
  36.         // JRDataSource is the datasource that Jasper understands  
  37.         // This is basically a wrapper to Java's collection classes  
  38.         JRDataSource categoryData  = dataprovider.getCategoriesData();  
  39.   
  40.         // parameterMap is the Model of our application  
  41.         Map<String,Object> parameterMap = new HashMap<String,Object>();  
  42.           
  43.         // must have the empty data source!!!  
  44.         JREmptyDataSource emptyData = new JREmptyDataSource();  
  45.         parameterMap.put("datasource", emptyData);  
  46.         parameterMap.put("JasperfishSubReportDatasource", categoryData);  
  47.         parameterMap.put("JasperfishSummaryInfo", dataprovider.getSummaryInfo());  
  48.           
  49.         // pdfReport is the View of our application  
  50.         // This is declared inside the /WEB-INF/jasper-views.xml  
  51.         modelAndView = new ModelAndView("pdfReport", parameterMap);  
  52.           
  53.         // Return the View and the Model combined  
  54.         return modelAndView;  
  55.     }  
  56. }  
Mock数据工厂代码如下:

[java]  view plain copy
  1. package com.gloomyfish.report.dao;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.ArrayList;  
  5. import java.util.Date;  
  6. import java.util.List;  
  7.   
  8. import net.sf.jasperreports.engine.JRDataSource;  
  9. import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;  
  10.   
  11. public class MockDataFactory {  
  12.       
  13.     public MockDataFactory()  
  14.     {  
  15.         System.out.println("create mock up data");  
  16.     }  
  17.       
  18.     public GloomyFishSummaryInfoBean getSummaryInfo()  
  19.     {  
  20.         GloomyFishSummaryInfoBean summaryBean = new GloomyFishSummaryInfoBean();  
  21.         summaryBean.setBlogURL("http://blog.csdn.net/jia20003");  
  22.         summaryBean.setMajorDomain("J2SE, J2EE, WEB developer");  
  23.         summaryBean.setName("jia20003");  
  24.         summaryBean.setNickName("gloomyfish");  
  25.         summaryBean.setRegDate("2003-03-02");  
  26.         summaryBean.setWorkYears(8);  
  27.         return summaryBean;  
  28.     }  
  29.       
  30.     public JRDataSource getCategoriesData()  
  31.     {  
  32.         List<ArticlesCategory> listData = new ArrayList<ArticlesCategory>();  
  33.         SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy, hh:mm:ss");  
  34.         Date createDte = new Date();  
  35.         ArticlesCategory category1 = new ArticlesCategory();  
  36.         ArticlesCategory category2 = new ArticlesCategory();  
  37.         ArticlesCategory category3 = new ArticlesCategory();  
  38.         ArticlesCategory category4 = new ArticlesCategory();  
  39.         ArticlesCategory category5 = new ArticlesCategory();  
  40.         ArticlesCategory category6 = new ArticlesCategory();  
  41. //      ArticlesCategory category7 = new ArticlesCategory();  
  42. //      ArticlesCategory category8 = new ArticlesCategory();  
  43. //      ArticlesCategory category9 = new ArticlesCategory();  
  44. //      ArticlesCategory categoryTen = new ArticlesCategory();  
  45.         category1.setCategoryName("Android Development");  
  46.         category1.setCount(6);  
  47.         category1.setCreateDate(sdf.format(createDte));  
  48.         category2.setCategoryName("Swing Desktop Development");  
  49.         category2.setCount(21);  
  50.         category2.setCreateDate(sdf.format(createDte));  
  51.         category3.setCategoryName("JAVA 2D Image Process");  
  52.         category3.setCount(56);  
  53.         category3.setCreateDate(sdf.format(createDte));  
  54.         category4.setCategoryName("J2EE");  
  55.         category4.setCount(8);  
  56.         category4.setCreateDate(sdf.format(createDte));  
  57.         category5.setCategoryName("HTML5");  
  58.         category5.setCount(4);  
  59.         category5.setCreateDate(sdf.format(createDte));  
  60.         category6.setCategoryName("Network Protocols Research");  
  61.         category6.setCount(4);  
  62.         category6.setCreateDate(sdf.format(createDte));  
  63.         category6.setCategoryName("ExtJS Learning");  
  64.         category6.setCount(2);  
  65.         category6.setCreateDate(sdf.format(createDte));  
  66.         listData.add(category1);  
  67.         listData.add(category2);  
  68.         listData.add(category3);  
  69.         listData.add(category4);  
  70.         listData.add(category5);  
  71.         listData.add(category6);  
  72.         JRBeanCollectionDataSource data = new JRBeanCollectionDataSource(listData);  
  73.         return data;  
  74.     }  
  75.   
  76. }  
启动运行在浏览器中访问地址为:http://localhost:8080/express/hello.html

六:Deploy与运行

全部代码完成以后,从IDE中运行Maven的clean与install命令,得到war包

将war拷贝到tomcat的webapps下面即可启动tomcat然后从浏览器访问。

点击[Get PDF Report]会自动在新窗口中打开生成的PDF报表文件。

程序运行打开主页面结果如下:


获取PDF报表在浏览器中打开以后效果如下:


七:常见问题

1.      必须在applicationContext.xml中导入jasper-views.xml资源否则报表不会被编译

          为jasper文件

2.      Web.xml的servlet名必须与spring的xxx-servlet.xml中的xxx一致

3.      Jasper-views.xml中声明的子报表路径参数与数据参数必须与报表文件jrxml中保

         持一致

4.      报表中field变量名必须与Java Class中的field变量名一一对应。

 

八:项目文件打包下载,解压缩作为Maven项目导入以后运行clean与 install命令。

下载地址:http://download.csdn.net/detail/jia20003/4963552

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值