本文主要是介绍jasperReports 与spring mvc整合 及 图片显示问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
在本教程中,我们将学习如何在一个简单的Spring MVC Web应用中集成Jasper报表工具。教程涵盖的技术点有:报表自定义数据源的使用;报表渲染、呈现的配置与实现;iReport报表设计器的使用(报表模板设计)等。
代码下载
开发环境准备
以下两种方式任选其一:
1. 在已有的SpringMVC项目环境中进行配置(要求项目可正常运行、调试);
2. 在eclipse中安装Spring的STS工具,然后新建一个Spring MVC Project(要求已配置好Maven环境);


项目依赖包
添加必须的jasperreports包依赖:
-
- <dependency>
- <groupId>net.sf.jasperreports</groupId>
- <artifactId>jasperreports</artifactId>
- <version>5.6.0</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.groovy</groupId>
- <artifactId>groovy-all</artifactId>
- <version>2.2.2</version>
- </dependency>
SpringMvc JasperReport整合
1、继承JasperReportsMultiFormatView类,并重写fillReport()方法,在该方法中增加setUrl()实现,这样就可以在controller中指定要使用的报表模板文件了。这样做的好处是,只需要一个jasperReport配置文件,可以在controller中动态的设定报表模板url。
- package com.pes_soft.example;
-
- import java.util.Map;
-
- import net.sf.jasperreports.engine.JasperPrint;
- import net.sf.jasperreports.engine.JasperReport;
-
- import org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView;
-
-
-
-
-
-
- public class ApplicationIReportView extends JasperReportsMultiFormatView {
- private JasperReport jasperReport;
-
- public ApplicationIReportView() {
- super();
- }
-
- protected JasperPrint fillReport(Map<String, Object> model) throws Exception {
- if (model.containsKey("url")) {
- setUrl(String.valueOf(model.get("url")));
- this.jasperReport = loadReport();
- }
-
- return super.fillReport(model);
- }
-
- protected JasperReport getReport() {
- return this.jasperReport;
- }
- }
2、在/WEB-INF/jasper/目录下创建报表视图配置文件jasper-defs.xml,并指定解析器类为自定义的视图解析器类:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
-
- <bean id="iReportView" class="com.pes_soft.example.ApplicationIReportView">
-
- <property name="reportDataKey" value="jrMainDataSource"/>
- </bean>
- </beans>
3、Jasper报表的渲染需要用到XmlViewResolver视图解析器,这样你的项目中就会存在多个视图解析器。需要注意的是,项目中如果使用了多个视图解析器,则需要设置order的值来区分解析器的使用顺序,order值越小则越靠前。增加XmlViewResolver视图解析器的同时,并指定其要解析的配置文件路径。
那么,需要在/WEB-INF/spring/appServlet/servlet-context.xml中增加下述配置:
-
- <beans:bean id="jasperReportResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
- <beans:property name="order">
- <beans:value>0</beans:value>
- </beans:property>
- <beans:property name="location">
- <beans:value>WEB-INF/jasper/jasper-defs.xml</beans:value>
- </beans:property>
- </beans:bean>
测试准备
1、创建JavaBeanPerson实体类:
- package com.pes_soft.example.model;
-
- import java.util.ArrayList;
- import java.util.List;
-
-
-
-
-
-
- public class JavaBeanPerson {
- private String name;
- private String sex;
- private int age;
- private String hometown;
- private String phone;
-
- public JavaBeanPerson() {}
-
- public JavaBeanPerson(String name, String sex, int age, String hometown, String phone) {
- this.name = name;
- this.sex = sex;
- this.age = age;
- this.hometown = hometown;
- this.phone = phone;
- }
-
-
-
- public static List<JavaBeanPerson> getList() {
- List<JavaBeanPerson> list = new ArrayList<JavaBeanPerson>();
- list.add(new JavaBeanPerson("Lily", "female", 22, "Hubei", "10086"));
- list.add(new JavaBeanPerson("Macro", "male", 33, "Beijing", "13800000000"));
- list.add(new JavaBeanPerson("Andy", "male", 44, "HongKong", "13812345678"));
- list.add(new JavaBeanPerson("Linder", "female", 28, "Guangxi", "18677778888"));
- list.add(new JavaBeanPerson("Jessie", "female", 26, "Gansu", "18219177720"));
- return list;
- }
- }
2、使用iReport设计报表模板,并编成.jasper二进制文件,并拷贝至\WEB-INF\jasper目录下:

3、编写Controller方法
-
-
-
-
-
- @RequestMapping(value = "/report", method = RequestMethod.GET)
- public String report(Model model) {
-
- JRDataSource jrDataSource = new JRBeanCollectionDataSource(JavaBeanPerson.getList());
-
-
- model.addAttribute("url", "/WEB-INF/jasper/MvcIReportExample.jasper");
- model.addAttribute("format", "pdf");
- model.addAttribute("jrMainDataSource", jrDataSource);
-
- return "iReportView";
- }
运行结果

这篇关于jasperReports 与spring mvc整合 及 图片显示问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!