</pre><p></p><p>jasper-views.xml</p><pre name="code" class="html"><?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.2.xsd">
<bean id="reportView" class="com.weixin.yzh.controller.wsTest.CustomReportView">
<!-- <property name="url" value="/WEB-INF/jasper/report2.jasper"/> -->
<property name="reportDataKey" value="jrMainDataSource"/>
<property name="exporterParameters"> <!-- 渲染时的相关参数 -->
<map>
<entry key="net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI" value="../image?image="/> <!-- HTML时的白色背景图片(px)路径 -->
<entry key="net.sf.jasperreports.engine.export.JRHtmlExporterParameter.CHARACTER_ENCODING" value="UTF-8"/>
</map>
</property>
</bean>
</beans>
自定义JasperReportsMultiFormatView:
package com.weixin.yzh.controller.wsTest;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;
import org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView;
public class CustomReportView extends JasperReportsMultiFormatView {
private JasperReport report;
public CustomReportView() {
super();
}
protected JasperPrint fillReport(Map<String, Object> model) throws Exception {
if (model.containsKey("url")) {
setUrl(String.valueOf(model.get("url")));
this.report = loadReport();
}
return super.fillReport(model);
}
protected JasperReport getReport() {
return this.report;
}
// @Override
// protected void renderReport(JasperPrint populatedReport,
// Map<String, Object> model, HttpServletResponse response)
// throws Exception {
// if (model.containsKey("requestObject")) {
// HttpServletRequest request = (HttpServletRequest) model.get("requestObject");
// request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, populatedReport);
// }
// super.renderReport(populatedReport, model, response);
// }
/*@Override
protected void postProcessReport(JasperPrint populatedReport, Map model)
throws Exception {
// HTML reports need the JasperPrint object in the session
if ("html".equalsIgnoreCase((String) model.get("format"))) {
session.setAttribute(
ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,
populatedReport);
}
}*/
}
servlet里面的配置
<!-- 注册XmlViewResolver,用于iReport & JasperReports报表生成 -->
<bean id="jasperReportResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="order">
<value>0</value>
</property>
<property name="location">
<value>classpath:/src-resources/jasper-views.xml</value>
</property>
</bean>
web.xml里面的配置
<!-- JasperReport包中自带的Servlet,用来做为HTML形式显示报表的时候报表的图片,要在这里配置上,图片在jasperReport包中 -->
<servlet>
<servlet-name>JasperReportImageServlet</servlet-name>
<servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JasperReportImageServlet</servlet-name>
<url-pattern>/image</url-pattern>
</servlet-mapping>
测试代码:
@RequestMapping("/customerReport2")
public String report(Model model) {
List<TasteModel> tasteModelList = tasteModelService.queryTasteModelAll();
// 报表数据源
JRDataSource jrDataSource = new JRBeanCollectionDataSource(tasteModelList);
// 动态指定报表模板url
model.addAttribute("url", "/WEB-INF/jasper/TasteModelTest.jasper");
model.addAttribute("format", "html"); // 报表格式
model.addAttribute("jrMainDataSource", jrDataSource);
// <property name="IMAGES_URI" value="/servlets/image?image="/>
return "reportView"; // 对应jasper-views.xml中的bean id
}