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

简介: 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.      最终的项目目录结构如下:

1357399082_9667.png


二:Spring配置详解


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

1357399152_1171.png


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

1357399179_3161.png


三:Jasper Report配置详解


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

1357399193_4642.png


四:Report内容与数据源


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


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


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


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


五:Controller与注解


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


Controller代码如下:

package com.gloomyfish.express.controllers;
 
import java.util.HashMap;
import java.util.Map;
 
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
 
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
 
import com.gloomyfish.report.dao.MockDataFactory;
 
@Controller
public class JasperReportController {
  
  protected static Logger logger = Logger.getLogger("controller");
  
    /**
     * Retrieves the PDF report file
     * 
     * @return
     */
    @RequestMapping(value = "/getpdfReport", method = RequestMethod.GET)
    public ModelAndView doSalesReportPDF(ModelAndView modelAndView) 
     {
    logger.debug("Received request to download PDF report");
    
    // Retrieve our data from a mock data provider
    MockDataFactory dataprovider = new MockDataFactory();
    
    // Assign the datasource to an instance of JRDataSource
    // JRDataSource is the datasource that Jasper understands
    // This is basically a wrapper to Java's collection classes
    JRDataSource categoryData  = dataprovider.getCategoriesData();
 
    // parameterMap is the Model of our application
    Map<String,Object> parameterMap = new HashMap<String,Object>();
    
    // must have the empty data source!!!
    JREmptyDataSource emptyData = new JREmptyDataSource();
    parameterMap.put("datasource", emptyData);
    parameterMap.put("JasperfishSubReportDatasource", categoryData);
    parameterMap.put("JasperfishSummaryInfo", dataprovider.getSummaryInfo());
    
    // pdfReport is the View of our application
    // This is declared inside the /WEB-INF/jasper-views.xml
    modelAndView = new ModelAndView("pdfReport", parameterMap);
    
    // Return the View and the Model combined
    return modelAndView;
  }
}

Mock数据工厂代码如下:

package com.gloomyfish.report.dao;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
 
public class MockDataFactory {
  
  public MockDataFactory()
  {
    System.out.println("create mock up data");
  }
  
  public GloomyFishSummaryInfoBean getSummaryInfo()
  {
    GloomyFishSummaryInfoBean summaryBean = new GloomyFishSummaryInfoBean();
    summaryBean.setBlogURL("http://blog.csdn.net/jia20003");
    summaryBean.setMajorDomain("J2SE, J2EE, WEB developer");
    summaryBean.setName("jia20003");
    summaryBean.setNickName("gloomyfish");
    summaryBean.setRegDate("2003-03-02");
    summaryBean.setWorkYears(8);
    return summaryBean;
  }
  
  public JRDataSource getCategoriesData()
  {
    List<ArticlesCategory> listData = new ArrayList<ArticlesCategory>();
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy, hh:mm:ss");
    Date createDte = new Date();
    ArticlesCategory category1 = new ArticlesCategory();
    ArticlesCategory category2 = new ArticlesCategory();
    ArticlesCategory category3 = new ArticlesCategory();
    ArticlesCategory category4 = new ArticlesCategory();
    ArticlesCategory category5 = new ArticlesCategory();
    ArticlesCategory category6 = new ArticlesCategory();
//    ArticlesCategory category7 = new ArticlesCategory();
//    ArticlesCategory category8 = new ArticlesCategory();
//    ArticlesCategory category9 = new ArticlesCategory();
//    ArticlesCategory categoryTen = new ArticlesCategory();
    category1.setCategoryName("Android Development");
    category1.setCount(6);
    category1.setCreateDate(sdf.format(createDte));
    category2.setCategoryName("Swing Desktop Development");
    category2.setCount(21);
    category2.setCreateDate(sdf.format(createDte));
    category3.setCategoryName("JAVA 2D Image Process");
    category3.setCount(56);
    category3.setCreateDate(sdf.format(createDte));
    category4.setCategoryName("J2EE");
    category4.setCount(8);
    category4.setCreateDate(sdf.format(createDte));
    category5.setCategoryName("HTML5");
    category5.setCount(4);
    category5.setCreateDate(sdf.format(createDte));
    category6.setCategoryName("Network Protocols Research");
    category6.setCount(4);
    category6.setCreateDate(sdf.format(createDte));
    category6.setCategoryName("ExtJS Learning");
    category6.setCount(2);
    category6.setCreateDate(sdf.format(createDte));
    listData.add(category1);
    listData.add(category2);
    listData.add(category3);
    listData.add(category4);
    listData.add(category5);
    listData.add(category6);
    JRBeanCollectionDataSource data = new JRBeanCollectionDataSource(listData);
    return data;
  }
 
}

启动运行在浏览器中访问地址为:http://localhost:8080/express/hello.html

六:Deploy与运行


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


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


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


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

1357399535_2802.png



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

1357399598_4462.png



七:常见问题


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


转载请注明-2013-01-05

相关文章
|
8月前
|
数据可视化 Java BI
将 Spring 微服务与 BI 工具集成:最佳实践
本文探讨了 Spring 微服务与商业智能(BI)工具集成的潜力与实践。随着微服务架构和数据分析需求的增长,Spring Boot 和 Spring Cloud 提供了构建可扩展、弹性服务的框架,而 BI 工具则增强了数据可视化与实时分析能力。文章介绍了 Spring 微服务的核心概念、BI 工具在企业中的作用,并深入分析了两者集成带来的优势,如实时数据处理、个性化报告、数据聚合与安全保障。同时,文中还总结了集成过程中的最佳实践,包括事件驱动架构、集中配置管理、数据安全控制、模块化设计与持续优化策略,旨在帮助企业构建高效、智能的数据驱动系统。
410 1
将 Spring 微服务与 BI 工具集成:最佳实践
|
10月前
|
XML 人工智能 Java
Spring Boot集成Aviator实现参数校验
Aviator是一个高性能、轻量级的Java表达式求值引擎,适用于动态表达式计算。其特点包括支持多种运算符、函数调用、正则匹配、自动类型转换及嵌套变量访问,性能优异且依赖小。适用于规则引擎、公式计算和动态脚本控制等场景。本文介绍了如何结合Aviator与AOP实现参数校验,并附有代码示例和仓库链接。
645 0
|
7月前
|
缓存 安全 Java
《深入理解Spring》过滤器(Filter)——Web请求的第一道防线
Servlet过滤器是Java Web核心组件,可在请求进入容器时进行预处理与响应后处理,适用于日志、认证、安全、跨域等全局性功能,具有比Spring拦截器更早的执行时机和更广的覆盖范围。
|
7月前
|
前端开发 Java 微服务
《深入理解Spring》:Spring、Spring MVC与Spring Boot的深度解析
Spring Framework是Java生态的基石,提供IoC、AOP等核心功能;Spring MVC基于其构建,实现Web层MVC架构;Spring Boot则通过自动配置和内嵌服务器,极大简化了开发与部署。三者层层演进,Spring Boot并非替代,而是对前者的高效封装与增强,适用于微服务与快速开发,而深入理解Spring Framework有助于更好驾驭整体技术栈。
|
8月前
|
存储 安全 Java
如何在 Spring Web 应用程序中使用 @SessionScope 和 @RequestScope
Spring框架中的`@SessionScope`和`@RequestScope`注解用于管理Web应用中的状态。`@SessionScope`绑定HTTP会话生命周期,适用于用户特定数据,如购物车;`@RequestScope`限定于单个请求,适合无状态、线程安全的操作,如日志记录。合理选择作用域能提升应用性能与可维护性。
355 1
|
8月前
|
监控 Cloud Native Java
Spring Integration 企业集成模式技术详解与实践指南
本文档全面介绍 Spring Integration 框架的核心概念、架构设计和实际应用。作为 Spring 生态系统中的企业集成解决方案,Spring Integration 基于著名的 Enterprise Integration Patterns(EIP)提供了轻量级的消息驱动架构。本文将深入探讨其消息通道、端点、过滤器、转换器等核心组件,以及如何构建可靠的企业集成解决方案。
758 0
|
9月前
|
存储 NoSQL Java
探索Spring Boot的函数式Web应用开发
通过这种方式,开发者能以声明式和函数式的编程习惯,构建高效、易测试、并发友好的Web应用,同时也能以较小的学习曲线迅速上手,因为这些概念与Spring Framework其他部分保持一致性。在设计和编码过程中,保持代码的简洁性和高内聚性,有助于维持项目的可管理性,也便于其他开发者阅读和理解。
252 0
|
10月前
|
前端开发 Java API
Spring Cloud Gateway Server Web MVC报错“Unsupported transfer encoding: chunked”解决
本文解析了Spring Cloud Gateway中出现“Unsupported transfer encoding: chunked”错误的原因,指出该问题源于Feign依赖的HTTP客户端与服务端的`chunked`传输编码不兼容,并提供了具体的解决方案。通过规范Feign客户端接口的返回类型,可有效避免该异常,提升系统兼容性与稳定性。
713 0
|
存储 设计模式 前端开发
什么是SpringMVC?简单好理解!什么是应用分层?SpringMVC与应用分层的关系? 什么是三层架构?SpringMVC与三层架构的关系?
文章解释了SpringMVC的概念和各部分功能,探讨了应用分层的原因和具体实施的三层架构,以及SpringMVC与三层架构之间的关系和联系。
1064 1
什么是SpringMVC?简单好理解!什么是应用分层?SpringMVC与应用分层的关系? 什么是三层架构?SpringMVC与三层架构的关系?

热门文章

最新文章