springmvc怎么配置jasper
展开全部
开发环境准备
以下两种方式任选其一:
1. 在已有的SpringMVC项目环境中进行配置(要求项目可正常运行、调试);
2. 在eclipse中安装Spring的STS工具,然后新建一个Spring MVC Project(要求已配置好Maven环境);
项目依赖包
添加必须的jasperreports包依赖:
[html] view plain copy
<!-- iReport 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。
[java] view plain copy
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;
/**
* SpringMVC + IReport整合 视图处理扩展
* @Author 许亮
* @Create 2015-11-7 21:38:18
*/
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,并指定解析器类为自定义的视图解析器类:
[html] view plain copy
<?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="url" value="/WEB-INF/jasper/report2.jasper"/> -->
<property name="reportDataKey" value="jrMainDataSource"/>
</bean>
</beans>
3、Jasper报表的渲染需要用到XmlViewResolver视图解析器,这样你的项目中就会存在多个视图解析器。需要注意的是,项目中如果使用了多个视图解析器,则需要设置order的值来区分解析器的使用顺序,order值越小则越靠前。增加XmlViewResolver视图解析器的同时,并指定其要解析的配置文件路径。
那么,需要在/WEB-INF/spring/appServlet/servlet-context.xml中增加下述配置:
[html] view plain copy
<!-- 注册XmlViewResolver,用于iReport & JasperReports报表生成 -->
<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实体类:
[java] view plain copy
package com.pes_soft.example.model;
import java.util.ArrayList;
import java.util.List;
/**
* iReport测试bean: Person实体类
* @author 许亮
* @Create 2015-1-15 20:32:27
*/
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;
}
// 此处省略字段的getter和setter
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方法
[java] view plain copy
/**
* 返回iReport报表视图
* @param model
* @return
*/
@RequestMapping(value = "/report", method = RequestMethod.GET)
public String report(Model model) {
// 报表数据源
JRDataSource jrDataSource = new JRBeanCollectionDataSource(JavaBeanPerson.getList());
// 动态指定报表模板url
model.addAttribute("url", "/WEB-INF/jasper/MvcIReportExample.jasper");
model.addAttribute("format", "pdf"); // 报表格式
model.addAttribute("jrMainDataSource", jrDataSource);
return "iReportView"; // 对应jasper-defs.xml中的bean id
}
运行结果
以下两种方式任选其一:
1. 在已有的SpringMVC项目环境中进行配置(要求项目可正常运行、调试);
2. 在eclipse中安装Spring的STS工具,然后新建一个Spring MVC Project(要求已配置好Maven环境);
项目依赖包
添加必须的jasperreports包依赖:
[html] view plain copy
<!-- iReport 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。
[java] view plain copy
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;
/**
* SpringMVC + IReport整合 视图处理扩展
* @Author 许亮
* @Create 2015-11-7 21:38:18
*/
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,并指定解析器类为自定义的视图解析器类:
[html] view plain copy
<?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="url" value="/WEB-INF/jasper/report2.jasper"/> -->
<property name="reportDataKey" value="jrMainDataSource"/>
</bean>
</beans>
3、Jasper报表的渲染需要用到XmlViewResolver视图解析器,这样你的项目中就会存在多个视图解析器。需要注意的是,项目中如果使用了多个视图解析器,则需要设置order的值来区分解析器的使用顺序,order值越小则越靠前。增加XmlViewResolver视图解析器的同时,并指定其要解析的配置文件路径。
那么,需要在/WEB-INF/spring/appServlet/servlet-context.xml中增加下述配置:
[html] view plain copy
<!-- 注册XmlViewResolver,用于iReport & JasperReports报表生成 -->
<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实体类:
[java] view plain copy
package com.pes_soft.example.model;
import java.util.ArrayList;
import java.util.List;
/**
* iReport测试bean: Person实体类
* @author 许亮
* @Create 2015-1-15 20:32:27
*/
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;
}
// 此处省略字段的getter和setter
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方法
[java] view plain copy
/**
* 返回iReport报表视图
* @param model
* @return
*/
@RequestMapping(value = "/report", method = RequestMethod.GET)
public String report(Model model) {
// 报表数据源
JRDataSource jrDataSource = new JRBeanCollectionDataSource(JavaBeanPerson.getList());
// 动态指定报表模板url
model.addAttribute("url", "/WEB-INF/jasper/MvcIReportExample.jasper");
model.addAttribute("format", "pdf"); // 报表格式
model.addAttribute("jrMainDataSource", jrDataSource);
return "iReportView"; // 对应jasper-defs.xml中的bean id
}
运行结果
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询