论坛首页 入门技术论坛

请教spring2+ibatis2找不到数据源

浏览 2473 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-09-25  
http://localhost:8888/spring/db.do

错误提示
HTTP Status 500 - 

--------------------------------------------------------------------------------

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:487)
	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:430)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)


root cause 

java.lang.NullPointerException
	lsj.EmpSqlMapDao.getAllEmp(Unknown Source)
	lsj.MyDb.handleRequest(Unknown Source)
	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:857)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:475)
	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:430)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.23 logs.


--------------------------------------------------------------------------------

Apache Tomcat/5.5.23


dispatcher-servlet.xml
<?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-2.0.xsd
">

	<bean id="viewResolve"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<bean name="/hello.do" class="lsj.MyC">
		<property name="viewPage" value="hello" />
	</bean>

	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"
			value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
		<property name="url"
			value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind" />
		<property name="username" value="sa" />
		<property name="password" value="" />
	</bean>

	<bean id="sqlMapClient"
		class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation"
			value="WEB-INF/classes/lsj/sql-map-config.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>

	<bean id="empDao" class="lsj.EmpSqlMapDao">
		<property name="sqlMapClient" ref="sqlMapClient"/>
	</bean>

	<bean name="/db.do" class="lsj.MyDb">
		<property name="viewPage" value="db" />
	</bean>
	
</beans>
   发表时间:2007-09-26  
package lsj;

public class Emp {
  private String EmployeeID,LastName,FirstName;

  public String getEmployeeID() {
    return EmployeeID;
  }

  public void setEmployeeID(String employeeID) {
    EmployeeID = employeeID;
  }

  public String getLastName() {
    return LastName;
  }

  public void setLastName(String lastName) {
    LastName = lastName;
  }

  public String getFirstName() {
    return FirstName;
  }

  public void setFirstName(String firstName) {
    FirstName = firstName;
  }
  
}


package lsj;

import java.util.List;

import lsj.Emp;

public interface EmpDao {
  public List<Emp> getAllEmp();
}


package lsj;

import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.DataAccessException;

import com.ibatis.sqlmap.client.SqlMapClient;

public class EmpSqlMapDao implements EmpDao {

  private SqlMapClient sqlMapClient;

  public void setSqlMapClient(SqlMapClient sqlMapClient) {
    this.sqlMapClient = sqlMapClient;
  }

  public List<Emp> getAllEmp() throws DataAccessException {
    try {
      return (List<Emp>) this.sqlMapClient.queryForList("getAllEmp");
    } catch (SQLException se) {
      se.toString();
    }
    return null;
  }
}


package lsj;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

public class MyDb implements Controller {
  private String viewPage;

  public void setViewPage(String viewPage) {
    this.viewPage = viewPage;
  }

  public ModelAndView handleRequest(HttpServletRequest arg0,
      HttpServletResponse arg1) throws Exception {
    ModelAndView modelAndView = new ModelAndView("db");
    modelAndView.addObject("empDao", new EmpSqlMapDao().getAllEmp());
    return modelAndView;
  }

}


Emp.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="Emp">

  <typeAlias alias="emp" type="lsj.Emp" />

  <select id="getAllEmp" resultClass="emp">
    SELECT EmployeeID, LastName, FirstName FROM Employees
  </select>

</sqlMap>


sql-map-config.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig      
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"      
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

  <sqlMap resource="lsj/Emp.xml" />

</sqlMapConfig>


db.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head>

<body>

</body>
</html>
0 请登录后投票
论坛首页 入门技术版

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