JAsperreportとspringの統合


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;
  }

}



文字化けしているが,どうせはっきり言うつもりだ.