论坛首页 Java企业应用论坛

jasperreport跟spring的集成

浏览 5310 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-09-02  
在spring 2.0以上版本,提供了对jasper report的集成功能
可以参考 manning 的spring in action II,其中有一章专门讲解这个
另外,google查一下 spring 集成 jasper,也有不少的资料
概要如下
web.xml
<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>

建立jasperSpring-servlet.xml文件放到和web.xml一级
<?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="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"
			value="net.sourceforge.jtds.jdbc.Driver">
		</property>
		<property name="url"
			value="jdbc:jtds:sqlserver://10.200.250.110/Pinggu">
		</property>
		<property name="username" value="sa"></property>
		<property name="password" value=""></property>
	</bean>
	<bean id="publicUrlMapping"
		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="report">jasperController</prop>
			</props>
		</property>
	</bean>

	<bean id="jasperController"
		class="com.report.jinpin.jasper.JasperSpringController">
		<property name="dataSource">
			<ref local="dataSource" />
		</property>
	</bean>
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
		<property name="basename" value="views" />
	</bean>
</beans>




后台写一个servlet,包名自己取,和上面的文件配置中的保持一致
com.report.jinpin.jasper.JasperSpringController
package com.report.jinpin.jasper;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import net.sf.jasperreports.engine.JRResultSetDataSource;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class JasperSpringController implements Controller
{
  private DataSource dataSource;
  private String userid;
  private String schoolid;
  private String classid;
  private String classtype;
  private String classcontent;

  public ModelAndView handleRequest(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException, ClassNotFoundException, SQLException
  {
	  schoolid="";
	  classid="";
	  classtype="";
	  classcontent="";
	  schoolid=request.getParameter("schoolid");
	  //ISO-8859-1
	  schoolid=new  String(schoolid.getBytes("ISO-8859-1"),"utf-8");
	  classid=request.getParameter("classid");
	  classid=new  String(classid.getBytes("ISO-8859-1"),"utf-8");

	  classtype=request.getParameter("classtype");
	  classtype=new  String(classtype.getBytes("ISO-8859-1"),"utf-8");
	  classcontent=request.getParameter("classcontent");
	  classcontent=new  String(classcontent.getBytes("ISO-8859-1"),"utf-8");
	  String viewname="";
   
	  if(classcontent.equals("璇剧▼淇℃伅")) 
	  {
		  if(classtype.equals("楂樿亴楂樹笓"))
		     viewname="JingPinClass_GZ";
		  else 
			 viewname="JingPinClass_B"; 
	  }
	  if(classcontent.equals("鎺堣鏁欏笀")) 
	  {
		  if(classtype.equals("楂樿亴楂樹笓"))
		     viewname="MainTeacher_GZ";
		  else 
			 viewname="MainTeacher_B"; 
	  }		  
	  if(classcontent.equals("鏁欏笀闃熶紞")) 
		  {
			  if(classtype.equals("楂樿亴楂樹笓"))
			     viewname="SubTeacher_GZ";
			  else 
				 viewname="SubTeacher_B"; 
		  }			  
	  if(classcontent.equals("璇剧▼鍐呭")) 
		  {
			  if(classtype.equals("鏈锛堢悊璁鸿绋嬶級"))
			     viewname="theorycontent";
			  else 
				 viewname="practice_B_Content"; 

		  }			  

	  return new ModelAndView(viewname, getModel());
  }

  private Map getModel() throws ClassNotFoundException, SQLException
  {
    Connection connection;
    Statement statement;
    ResultSet resultSet;
    HashMap model = new HashMap();
    
    String query =""; 
    //	"select * from JingPinClass_B where schoolId='鍖椾含澶у                  ' and classid='鍙h厰棰岄潰鍖诲褰卞儚璇婃柇瀛�";
    
  	  if(classcontent.equals("璇剧▼淇℃伅")) 
	  {
		  if(classtype.equals("楂樿亴楂樹笓"))
			  query="select * from JingPinClass_GZ where schoolId='"+schoolid+"' and classid='"+classid+"'";
			  
		  else 
			  query="select * from JingPinClass_B where schoolId='"+schoolid+"' and classid='"+classid+"'"; 
	  }
	  if(classcontent.equals("鎺堣鏁欏笀")) 
	  {
		  if(classtype.equals("楂樿亴楂樹笓"))
			  query="select * from MainTeacher_GZ where schoolId='"+schoolid+"' and classid='"+classid+"'";
		  else 
			  query="select * from MainTeacher_B where schoolId='"+schoolid+"' and classid='"+classid+"'"; 
	  }		  
	  if(classcontent.equals("鏁欏笀闃熶紞")) 
		  {
			  if(classtype.equals("楂樿亴楂樹笓"))
				  query="select * from SubTeacher_GZ where schoolId='"+schoolid+"' and classid='"+classid+"'";
				  
			  else 
				  query="select * from SubTeacher_B where schoolId='"+schoolid+"' and classid='"+classid+"'"; 
		  }			  
	  if(classcontent.equals("璇剧▼鍐呭") )
		  {
			  if(classtype.equals("鏈锛堢悊璁鸿绋嬶級"))
				  query="select * from ThoeryContent where schoolId='"+schoolid+"' and classid='"+classid+"'";
			  else 
				  query="select * from practice_B_Content where schoolId='"+schoolid+"' and classid='"+classid+"'"; 
		  }	   
    
    
    connection = dataSource.getConnection();
    statement = connection.createStatement();
    resultSet = statement.executeQuery(query);
    
    JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(resultSet);
    
    model.put("datasource", resultSetDataSource);
    
    
    return model;
  }
  
  public void setDataSource (DataSource dataSource)
  {
    this.dataSource=dataSource;
  }

}




有些乱码,反正意图是说清楚了
   发表时间:2008-12-15  
老大能不能给出个实例?
0 请登录后投票
   发表时间:2008-12-18  
这咋给你实例?我这个文章已经说的很清楚了啊
0 请登录后投票
   发表时间:2009-02-26  
如何在views.property中设置子报表?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics