« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| 公告 |
戒除浮躁,读好书,交益友 |
Blog信息 |
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9702877 建立时间:2004年12月20日 |

| |
[j2ee]spring使用DataSourceUtils.getConnection和JasperReport使用 原创空间
邢红瑞 发表于 2005/12/11 19:04:04 |
这是jetmaven的源码
/* * Copyright (c) 2004 Your Corporation. All Rights Reserved. */package com.jetmaven.oa;
import net.sf.jasperreports.engine.JasperExportManager;import net.sf.jasperreports.engine.JasperFillManager;import net.sf.jasperreports.engine.JasperPrint;import org.springframework.jdbc.datasource.DataSourceUtils;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.sql.DataSource;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.OutputStream;import java.sql.Connection;import java.util.Enumeration;import java.util.HashMap;import java.util.Map;
/** * @author <a href="yournamemailto:youmail@yourdomain.com">yourname</a> Date: 2004-11-22 */public class ViewJasperReportController implements Controller{ private DataSource dataSource;
public DataSource getDataSource() { return dataSource; }
public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; }
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { String relativeUri = request.getRequestURI().replaceFirst(request.getContextPath(), ""); File reportFilePath = new File(request.getSession(true).getServletContext().getRealPath(relativeUri)); if (reportFilePath.exists()) //jasper文件存在 { OutputStream out = response.getOutputStream(); response.setContentType("application/pdf"); Connection conn = DataSourceUtils.getConnection(dataSource); try { JasperPrint jasperPrint = JasperFillManager.fillReport(new FileInputStream(reportFilePath), convertRequestParamAndAttrToMap(request), conn); JasperExportManager.exportReportToPdfStream(jasperPrint, out); out.flush(); } finally { DataSourceUtils.closeConnectionIfNecessary(conn, dataSource); } } else //jasper文件不存在 { setError404(response); } return null; }
/** * 设置404错误,指定的jasper文件未找到 * * @param response web的response变量 * @throws java.io.IOException 异常 */ private void setError404(HttpServletResponse response) throws IOException { response.setContentType("text/html;charset=gb2312"); response.setStatus(404); response.getOutputStream().write("Error:404,File not Found!".getBytes()); }
/** * 将request的参数和属性转换成Map * * @param request web的request变量 * @return Map的对象 */ private Map convertRequestParamAndAttrToMap(HttpServletRequest request) { Map paramAndAttr = new HashMap(); //填充属性 Enumeration allAttrKey = request.getAttributeNames(); while (allAttrKey.hasMoreElements()) { String attrKey = (String) allAttrKey.nextElement(); paramAndAttr.put(attrKey, request.getAttribute(attrKey)); } return paramAndAttr; }} |
|
|