jasperreports+maven+springmvc
文章源自:http://blog.csdn.net/xht555/article/details/43409637
自己整合的时候遇到了好多坑,为了方便他人,也为了自己,在原文基础上写了这个东东。没事的时候可以看看。
1.把报表文件放到项目中指定位置
2.添加jar包(此处为了防止缺少jar包,我把pom.xml全部贴出来了)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0"?> 2 <project 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 4 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 5 <modelVersion>4.0.0</modelVersion> 6 <parent> 7 <groupId>com.fastcnt.creditx</groupId> 8 <artifactId>creditx-parent</artifactId> 9 <version>1.0.0</version> 10 </parent> 11 <artifactId>creditx-web</artifactId> 12 <version>1.0.0</version> 13 <packaging>war</packaging> 14 <name>creditx-web Maven Webapp</name> 15 <url>http://maven.apache.org</url> 16 <dependencies> 17 18 <dependency> 19 <groupId>${project.groupId}</groupId> 20 <artifactId>creditx-api</artifactId> 21 </dependency> 22 23 <dependency> 24 <groupId>com.alibaba</groupId> 25 <artifactId>dubbo</artifactId> 26 </dependency> 27 28 <dependency> 29 <groupId>com.101tec</groupId> 30 <artifactId>zkclient</artifactId> 31 </dependency> 32 33 <dependency> 34 <groupId>org.apache.zookeeper</groupId> 35 <artifactId>zookeeper</artifactId> 36 </dependency> 37 38 <!-- spring --> 39 <dependency> 40 <groupId>org.springframework</groupId> 41 <artifactId>spring-webmvc</artifactId> 42 </dependency> 43 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-jdbc</artifactId> 47 </dependency> 48 49 <dependency> 50 <groupId>org.springframework</groupId> 51 <artifactId>spring-orm</artifactId> 52 </dependency> 53 54 <dependency> 55 <groupId>org.springframework</groupId> 56 <artifactId>spring-context-support</artifactId> 57 </dependency> 58 <dependency> 59 <groupId>c3p0</groupId> 60 <artifactId>c3p0</artifactId> 61 </dependency> 62 63 <!-- jstl --> 64 <dependency> 65 <groupId>javax.servlet</groupId> 66 <artifactId>jstl</artifactId> 67 </dependency> 68 69 <!-- mybatis --> 70 <dependency> 71 <groupId>org.mybatis</groupId> 72 <artifactId>mybatis</artifactId> 73 </dependency> 74 75 <dependency> 76 <groupId>org.mybatis</groupId> 77 <artifactId>mybatis-spring</artifactId> 78 </dependency> 79 80 <dependency> 81 <groupId>mysql</groupId> 82 <artifactId>mysql-connector-java</artifactId> 83 </dependency> 84 85 <!-- jackson --> 86 <dependency> 87 <groupId>com.fasterxml.jackson.core</groupId> 88 <artifactId>jackson-annotations</artifactId> 89 </dependency> 90 91 <dependency> 92 <groupId>org.codehaus.jackson</groupId> 93 <artifactId>jackson-mapper-asl</artifactId> 94 </dependency> 95 96 <dependency> 97 <groupId>com.fasterxml.jackson.core</groupId> 98 <artifactId>jackson-core</artifactId> 99 </dependency> 100 101 <dependency> 102 <groupId>org.codehaus.jackson</groupId> 103 <artifactId>jackson-core-asl</artifactId> 104 </dependency> 105 106 <dependency> 107 <groupId>com.fasterxml.jackson.core</groupId> 108 <artifactId>jackson-databind</artifactId> 109 </dependency> 110 111 <dependency> 112 <groupId>org.apache.commons</groupId> 113 <artifactId>commons-lang3</artifactId> 114 </dependency> 115 116 <dependency> 117 <groupId>commons-fileupload</groupId> 118 <artifactId>commons-fileupload</artifactId> 119 </dependency> 120 121 <dependency> 122 <groupId>org.slf4j</groupId> 123 <artifactId>slf4j-api</artifactId> 124 </dependency> 125 126 <dependency> 127 <groupId>ch.qos.logback</groupId> 128 <artifactId>logback-classic</artifactId> 129 <scope>runtime</scope> 130 </dependency> 131 132 <!-- 代码直接调用log4j会被桥接到slf4j --> 133 <dependency> 134 <groupId>org.slf4j</groupId> 135 <artifactId>log4j-over-slf4j</artifactId> 136 <scope>runtime</scope> 137 </dependency> 138 <!-- 代码直接调用common-logging会被桥接到slf4j --> 139 <dependency> 140 <groupId>org.slf4j</groupId> 141 <artifactId>jcl-over-slf4j</artifactId> 142 <scope>runtime</scope> 143 </dependency> 144 <!-- 代码直接调用java.util.logging会被桥接到slf4j --> 145 <dependency> 146 <groupId>org.slf4j</groupId> 147 <artifactId>jul-to-slf4j</artifactId> 148 <scope>runtime</scope> 149 </dependency> 150 151 <dependency> 152 <groupId>net.sf.jasperreports</groupId> 153 <artifactId>jasperreports</artifactId> 154 </dependency> 155 <dependency> 156 <groupId>org.codehaus.groovy</groupId> 157 <artifactId>groovy-all</artifactId> 158 </dependency> 159 160 <dependency> 161 <groupId>com.lowagie</groupId> 162 <artifactId>itext</artifactId> 163 <version>2.1.7</version> 164 </dependency> 165 166 </dependencies> 167 <build> 168 <finalName>creditx-web</finalName> 169 </build> 170 </project>
3.添加一个视图解析器
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> 7 8 <bean id="reportView" class="controller.CustomReportView"> 9 <property name="reportDataKey" value="jrMainDataSource"/> 10 </bean> 11 </beans>
4.在spring-mvc中配置视图解析器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!-- 注册XmlViewResolver,用于iReport & JasperReports报表生成 --> 2 <bean id="jasperReportResolver" class="org.springframework.web.servlet.view.XmlViewResolver"> 3 <property name="order"> 4 <value>0</value> 5 </property> 6 <property name="location"> 7 <value>WEB-INF/jasper-views.xml</value> 8 </property> 9 </bean>
5.编写视图解析器,javabean数据源数据集
视图解析器:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package controller; 2 3 import java.util.Map; 4 5 import net.sf.jasperreports.engine.JasperPrint; 6 import net.sf.jasperreports.engine.JasperReport; 7 8 import org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView; 9 10 public class CustomReportView extends JasperReportsMultiFormatView { 11 private JasperReport report; 12 13 public CustomReportView() { 14 super(); 15 } 16 17 protected JasperPrint fillReport(Map<String, Object> model) throws Exception { 18 if (model.containsKey("url")) { 19 setUrl(String.valueOf(model.get("url"))); 20 this.report = loadReport(); 21 } 22 return super.fillReport(model); 23 } 24 25 protected JasperReport getReport() { 26 return this.report; 27 } 28 }
javabean:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package model; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 /** 7 * iReport测试bean: Person实体类 8 * @author 许亮 9 * @Create 2015-2-2 17:03:44 10 */ 11 public class JavaBeanPerson { 12 private String name; // 姓名 13 private String sex; // 性别 14 private int age; // 年龄 15 private String hometown;// 籍贯 16 private String phone; // 电话号码 17 18 public JavaBeanPerson() {} 19 20 public JavaBeanPerson(String name, String sex, int age, String hometown, String phone) { 21 this.name = name; 22 this.sex = sex; 23 this.age = age; 24 this.hometown = hometown; 25 this.phone = phone; 26 } 27 28 /** 29 * 姓名 30 * @return 31 */ 32 public String getName() { 33 return name; 34 } 35 36 /** 37 * 姓名 38 * @param name 39 */ 40 public void setName(String name) { 41 this.name = name; 42 } 43 44 /** 45 * 性别 46 * @return 47 */ 48 public String getSex() { 49 return sex; 50 } 51 52 /** 53 * 性别 54 * @param sex 55 */ 56 public void setSex(String sex) { 57 this.sex = sex; 58 } 59 60 /** 61 * 年龄 62 * @return 63 */ 64 public int getAge() { 65 return age; 66 } 67 68 /** 69 * 年龄 70 * @param age 71 */ 72 public void setAge(int age) { 73 this.age = age; 74 } 75 76 /** 77 * 籍贯 78 * @return 79 */ 80 public String getHometown() { 81 return hometown; 82 } 83 84 /** 85 * 籍贯 86 * @param hometown 87 */ 88 public void setHometown(String hometown) { 89 this.hometown = hometown; 90 } 91 92 /** 93 * 电话号码 94 * @return 95 */ 96 public String getPhone() { 97 return phone; 98 } 99 100 /** 101 * 电话号码 102 * @param phone 103 */ 104 public void setPhone(String phone) { 105 this.phone = phone; 106 } 107 108 public static List<JavaBeanPerson> getList() { 109 List<JavaBeanPerson> list = new ArrayList<JavaBeanPerson>(); 110 list.add(new JavaBeanPerson("Lily", "female", 22, "Hubei", "10086")); 111 list.add(new JavaBeanPerson("Macro", "male", 33, "Beijing", "13800000000")); 112 list.add(new JavaBeanPerson("Andy", "male", 44, "HongKong", "13812345678")); 113 list.add(new JavaBeanPerson("Linder", "female", 28, "Guangxi", "18677778888")); 114 list.add(new JavaBeanPerson("Jessie", "female", 26, "Gansu", "18219177720")); 115 return list; 116 } 117 }
6.访问
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package controller; 2 3 import java.text.DateFormat; 4 import java.util.Date; 5 import java.util.Locale; 6 7 import model.JavaBeanPerson; 8 import net.sf.jasperreports.engine.JRDataSource; 9 import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; 10 11 import org.slf4j.Logger; 12 import org.slf4j.LoggerFactory; 13 import org.springframework.stereotype.Controller; 14 import org.springframework.ui.Model; 15 import org.springframework.web.bind.annotation.RequestMapping; 16 import org.springframework.web.bind.annotation.RequestMethod; 17 18 /** 19 * Handles requests for the application home page. 20 */ 21 @Controller 22 public class HomeController { 23 24 private static final Logger logger = LoggerFactory 25 .getLogger(HomeController.class); 26 27 /** 28 * Simply selects the home view to render by returning its name. 29 */ 30 @RequestMapping(value = "/home", method = RequestMethod.GET) 31 public String home(Locale locale, Model model) { 32 logger.info("Welcome home! The client locale is {}.", locale); 33 34 Date date = new Date(); 35 DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, 36 DateFormat.LONG, locale); 37 38 String formattedDate = dateFormat.format(date); 39 40 model.addAttribute("serverTime", formattedDate); 41 42 return "/home"; 43 } 44 45 @RequestMapping(value = "/report", method = RequestMethod.GET) 46 public String report(Model model) { 47 // 报表数据源 48 JRDataSource jrDataSource = new JRBeanCollectionDataSource( 49 JavaBeanPerson.getList()); 50 51 // 动态指定报表模板url 52 model.addAttribute("url", "/WEB-INF/jasper/spring_report.jasper"); 53 // 动态指定报表模板url 54 model.addAttribute("format", "pdf"); // 报表格式 55 model.addAttribute("jrMainDataSource", jrDataSource); 56 57 return "reportView"; // 对应jasper-views.xml中的bean id 58 } 59 60 }
效果截图:
积极的人,象太阳,照到哪里哪里亮,消极的人,象月亮,初一十五不一样。想法决定我们的生活,有什么样的想法,就有什么样的未来。对于刚刚进入职场的我们,会经历很多不同的心境变化,很多时候现实跟你想象的都大相庭径,良好的心态很重要!