SpringMVC4.2.1+jasper+ireports5.6整合及部门问题解决方案

最近在学习整合Jasperreport到springMVC中,在网上找了很多大神的资料,终于能够正常运行了,把整合方案和遇到的问题整理如下:

1.软件

a) 下载iReport-5.6.0,只支持1.7-1.5版本的JDK,如果本机已经安装了1.7以上版本,请下载一个jdk1.7-1.5的版本,修改iReport-5.6.0\etc中的ireport.conf文档,将jdkhome的路径该为你下载的jdk安装路径

b) PDF中文显示问题:需要在需要中文展示的字段配置属性如下属性

c) net.sf.jasperreports.engine.JRException: Error retrieving field value from bean 异常解决方案:

i. 错误1检查你的JavaBean,在JavaBean中补充各属性需要的Set/Get方法(1必须有)

ii. 错误2:检查JavaBean中各字段是否与iReport报表中设计的一致(包括各字段对应的类型和总个数)。假设你的JavaBean中有user字段,表示“用户”,这里我们用字符串表示,在JavaBean中:private String user;iReport表格设计中,右键点击Fields”图标 —>“添加 Field(A)”,将新建的field1重命名为user”,点击“确定”即可,其他步骤省略...最终部分结果在XML中类型如下所示:

Java.lang.String"/>

... ...

... ...

iii. 错误:3删除,如果描述是空的

d) 图形重复问题:不能放在detail band中,放在其不重复的band中,如:summary band

e) Excel模式下多个jasper放在同一个Excel的方法,设置参数:

JRXlsExporter xls = new JRXlsExporter();
xls.setParameter(JRExporterParameter.JASPER_PRINT_LIST,list);//此处的list为JasperPrint的list集合 xls.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.TRUE);//设置为true,即可在一个excel中,每个单独的jasper对象放入到一个sheet页中
String[] sheetNames = {"自定义1","自定义2","自定义3"};
xls.setParameter(JRXlsExporterParameter.SHEET_NAMES, sheetNames);//此处这样写即可

2.Jar包配置

a) 配置包的maven如下

<dependency>

<groupId>net.sf.jasperreportsgroupId>

<artifactId>jasperreportsartifactId>

<version>5.6.0version>

dependency>

<dependency>

org.codehaus.groovy

groovy-all

2.2.2

dependency>

<dependency>

<groupId>com.lowagiegroupId>

<artifactId>itextasianartifactId>

<version>1.0version>

dependency>

<dependency>

<groupId>com.lowagiegroupId>

   <artifactId>itextasianartifactId>

<version>2.1.7.js2version>

dependency>

<dependency>

<groupId>org.springframeworkgroupId>

<artifactId>spring-context-supportartifactId>

<version>4.2.1.RELEASEversion>

dependency>

PS:其中itextasian-1.0.jar,itextasian-2.1.7.js2.jarmaven中没有,是自己上传的,jar包在iReport-5.6.0\ireport\modules\ext中有,直接上传到本地的maven服务器就可以

3.SpringMVC配置

a) 通用解析器代码 ApplicationIReportView.java

package com.farmer.view;

import java.util.Map;

 

import org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView;

import net.sf.jasperreports.engine.JasperPrint;

import net.sf.jasperreports.engine.JasperReport;

 

/**

 * SpringMVC + IReport整合 视图处理扩展

 * @Author 许亮

 * @Create 2015-11-7 21:38:18

 */

public class ApplicationIReportViewextends JasperReportsMultiFormatView {

public static final StringCSV = "csv";

public static final StringHTML = "html";

public static final StringPDF = "pdf";

public static final StringXLS = "xls";

public static final StringXLSX = "xlsx";

private JasperReportjasperReport;

public ApplicationIReportView() {

super();

}

 

protected JasperPrintfillReport(Map model) throws Exception {

if (model.containsKey("url")) {

setUrl(String.valueOf(model.get("url")));

this.jasperReport = loadReport();

}

return super.fillReport(model);

}

protected JasperReportgetReport() {

return this.jasperReport;

}

}

b) 视图配置 jasper-defs.xml

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">

c) SpringMVC配置文件增加试图解析器,放在spring-mvc.xml

0

classpath:jasper/jasper-defs.xml

d) 增加数据源代码

package com.farmer.factory;

public class PictureBean {

private String id;

private int value;

private long time;

public String getId() {

return id;

}

public void setId(Stringid) {

this.id =id;

}

public int getValue() {

return value;

}

public void setValue(int value) {

this.value =value;

}

public long getTime() {

return time;

}

public void setTime(long time) {

this.time =time;

}

}

 

e) 增加报表工厂的代码

package com.farmer.factory;

 

import java.util.ArrayList;

import java.util.List;

 

import com.farmer.api.bean.SdjsSiteConfig;

 

import net.sf.jasperreports.engine.JRDataSource;

import net.sf.jasperreports.engine.JRException;

import net.sf.jasperreports.engine.JasperReport;

Import net.sf.jasperreports.engine.data.JRAbstractBeanDataSourceProvider;

import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

 

public class SiteConditionFactory extends JRAbstractBeanDataSourceProvider{

 

 

public SiteConditionFactory() {

super(SdjsSiteConfig.class);

}

 

@Override

public JRDataSource create(JasperReport arg0) throws JRException {

List list = new ArrayList();

PictureBean bean = new PictureBean();

bean.setId("测试1号");

bean.setValue(5);

bean.setTime(10000);

list.add(bean);

PictureBean bean1 = new PictureBean();

bean1.setId("测试1号");

bean1.setValue(105);

bean1.setTime(20000);

list.add(bean1);

PictureBean bean2 = new PictureBean();

bean2.setId("测试2号");

bean2.setValue(27);

bean2.setTime(10000);

list.add(bean2);

PictureBean bean3 = new PictureBean();

bean3.setId("测试2号");

bean3.setValue(45);

bean3.setTime(20000);

list.add(bean3);

PictureBean bean4 = new PictureBean();

bean4.setId("测试3号");

bean4.setValue(51);

bean4.setTime(10000);

list.add(bean4);

return new JRBeanCollectionDataSource(list);

}

 

@Override

public void dispose(JRDataSource arg0) throws JRException {

}

}

 

f) Controller调用代码

package com.farmer.controller;

 

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

 

import com.farmer.factory.SiteConditionFactory;

import com.farmer.view.ApplicationIReportView;

 

import net.sf.jasperreports.engine.JRException;

 

@Controller

@RequestMapping("/reports")

public class ReportsController {

    

/**

 * 返回iReport报表视图

 * @param model

 * @return

 * @throws JRException

 */

@RequestMapping(value = "/getsiteconfig", method = RequestMethod.GET)

public String report(Model model) throws JRException {

// 报表数据源

SiteConditionFactory factory = new SiteConditionFactory();

// 动态指定报表模板url

model.addAttribute("url", "/WEB-INF/classes/jasper/SdjsTest.jasper");

model.addAttribute("format", ApplicationIReportView.PDF); // 报表格式

model.addAttribute("jrMainDataSource", factory.create(null));

//model.addAttribute("REPORT_MAX_COUNT",2);

return "iReportView";

}

}


你可能感兴趣的:(Jasperreport)