一个简单的ireport报表(用spring的servlet)

1.在web.xml中配置springMVC的servlet
<!-- 报表输出的 servlet,这是spring的servlet-->
   <servlet>   
        <servlet-name>jasperSpring</servlet-name>   
        <servlet-class>   
            org.springframework.web.servlet.DispatcherServlet   
        </servlet-class>   
        <load-on-startup>1</load-on-startup>   
    </servlet>   
    <servlet-mapping>   
        <servlet-name>jasperSpring</servlet-name>   
        <url-pattern>/jasperSpring/*</url-pattern>
    </servlet-mapping>


2.在WEB-INF目录下添加xml文件,文件名为:jasperSpring-servlet.xml (上面配置的servlet名加上“-servlet”);文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">   
  
<beans>   
   
    <!-- 映像处理器,转到相应的处理器 -->  
    <bean id="publicUrlMapping"  
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">   
        <property name="mappings">   
            <props>
            	<!-- 通过servlet名/key 如:   http:/.../jasperSpring/report -->   
                <prop key="report">jasperController</prop>   
            </props>   
        </property>   
    </bean>   
  
  <!-- 处理器  即servlet跳转到的类 -->
    <bean id="jasperController"  
        class="jasperController.JasperSpringController">
    </bean> 
    
    <!-- 解析器  即处理完成后要跳转的地方,这里在views.properties配置 -->  
    <bean id="viewResolver"  
        class="org.springframework.web.servlet.view.ResourceBundleViewResolver">   
        <property name="basename" value="views" />   
    </bean>  
    
</beans> 



3.配置views.properties,内容如下:
testReportByPdf.class         = org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView
#testReportByPdf.class    	  = org.springframework.web.servlet.view.jasperreports.JasperReportsHtmlView
testReportByPdf.reportDataKey = dataset
testReportByPdf.url           = /report/myReport.jasper


4.servlet处理类如下:
public class JasperSpringController implements Controller{

	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		String viewName = "testReportByPdf";
		Map param = new HashMap();
		
		List<TestModel> dataList = this.getData();		
		JRDataSource dataset = new JRBeanCollectionDataSource(dataList);
		param.put("dataset", dataset);
		return new ModelAndView(viewName,param);
	}
	
	/**
	 * 连数据库取数据
	 * @author yj
	 * @return
	 */
	private List<TestModel> getData() {
		List<TestModel> dataList = new ArrayList<TestModel>();
		Connection conn = null;
		Statement sta = null;
		ResultSet rs = null;
		
		try{
			Context context = new InitialContext();
			DataSource ds = (DataSource) context.lookup("java:/comp/env/MyDb");
			conn = ds.getConnection();
			sta = conn.createStatement();
			rs = sta.executeQuery("select * from a");
			while(rs.next()){
				TestModel tm = new TestModel();
				tm.setId(rs.getLong("id"));
				tm.setName(rs.getString("name"));
				tm.setType(rs.getString("type"));
				tm.setTotal(rs.getLong("total"));
				dataList.add(tm);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				if(conn!=null){
					conn.close();
					conn = null;
				}
				if(sta != null){
					sta.close();
					sta = null;
				}
				if(rs != null){
					rs.close();
					rs = null;
				}
			}catch(SQLException se){
				se.printStackTrace();
			}
			
		}
		return dataList;
	}

}


5.使用的jar包如下:
activation.jar
commons-collections-3.1.jar
jasperreports-3.7.0.jar
jasperreports-fonts-3.7.0.jar
jfreechart-1.0.12.jar
log4j-1.2.15.jar
ojdbc14.jar
spring.jar
commons-beanutils-1.8.0.jar
commons-digester-1.8.jar
spring-webmvc.jar
groovy-all-1.5.5.jar
iText-2.1.0.jar
iTextAsian.jar
iTextAsianCmaps.jar

你可能感兴趣的:(spring,Web,xml,servlet,jfreechart)