论坛首页 Java企业应用论坛

spring中service中findAll无法取到值的问题

浏览 2582 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-05-06   最后修改:2009-05-06
IcreaderService.java
package gate.service;

import gate.SessionFactory.HibernateSessionFactory;
import gate.dao.AdminDAO;
import gate.dao.IcreaderDAO;
import gate.model.Icreader;
import gate.service.VO.IcreaderVO;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class IcreaderService {
	public List<IcreaderVO> findAllIcreader() throws IllegalAccessException, InvocationTargetException{
		IcreaderDAO con = new IcreaderDAO();
		List<IcreaderVO> list = new ArrayList<IcreaderVO>();
		Iterator<Icreader> item = con.findAll().iterator();
		while(item.hasNext()){
			Icreader co = (Icreader)item.next();
			IcreaderVO cov = new IcreaderVO();
			
			BeanUtils.copyProperties(cov, co);
			list.add(cov);
		}
		HibernateSessionFactory.closeSession();
		return list;
	}
	public void saveIcreader(IcreaderVO cov) throws IllegalAccessException, InvocationTargetException{
		IcreaderDAO con = new IcreaderDAO();
		Icreader co = new Icreader();
		BeanUtils.copyProperties(co, cov);
		con.save(co);
	}
	public void deleteIcreader(Long cid){
		IcreaderDAO con = new IcreaderDAO();
		Icreader co = new Icreader();
		co.setId(cid);
		con.delete(co);
	}
	public void editIcreader(IcreaderVO cov) throws IllegalAccessException, InvocationTargetException{
		IcreaderDAO con = new IcreaderDAO();
		Icreader co = new Icreader();
		BeanUtils.copyProperties(co, cov);
		con.update(co);
	}
	public IcreaderVO getIcreaderByID(Long cid) throws IllegalAccessException, InvocationTargetException{
		IcreaderDAO con = new IcreaderDAO();
		Icreader co = con.findById(cid);
		IcreaderVO cov = new IcreaderVO();
		BeanUtils.copyProperties(cov, co);
		return cov;
	}

}

IcreaderDAO.java
package gate.dao;

import gate.model.Icreader;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class IcreaderDAO extends HibernateDaoSupport implements IIcreaderDAO {
	private static final Log log = LogFactory.getLog(IcreaderDAO.class);
	//property constants
	public static final String CARDNO = "cardno";
	public static final String ACCOUNTNO = "accountno";
	public static final String STUDENTCODE = "studentcode";
	public static final String NAME = "name";
	public static final String SEX = "sex";
	public static final String DEPTNO = "deptcode";
	public static final String TYPENO = "typeno";
	public static final String EXPIREDDATE = "expireddate";
	public static final String FLAG = "flag";

	public void save(Icreader transientInstance) {
		log.debug("saving Icreader instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Icreader persistentInstance) {
		log.debug("deleting Icreader instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public void update(Icreader icreader) {
		log.debug("deleting Icreader instance");
		try {
			getHibernateTemplate().update(icreader);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Icreader findById(java.lang.Long id) {
		log.debug("getting Icreader instance with id: " + id);
		try {
			Icreader instance = (Icreader) getHibernateTemplate().get(
					"com.hibernate.demo.Icreader", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Icreader instance) {
		log.debug("finding Icreader instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {

		log.debug("finding Icreader instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Icreader as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}

	}

	public List findByCardno(Object cardno) {
		return findByProperty(CARDNO, cardno);
	}

	public List findByAccountno(Object accountno) {
		return findByProperty(ACCOUNTNO, accountno);
	}

	public List findByStudentcode(Object studentcode) {
		return findByProperty(STUDENTCODE, studentcode);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	public List findByTypeno(Object typeno) {
		return findByProperty(TYPENO, typeno);
	}

	public List findByDeptno(Object deptno) {
		return findByProperty(DEPTNO, deptno);
	}

	public List findByExpireddate(Object expireddate) {
		return findByProperty(EXPIREDDATE, expireddate);
	}

	public List findByFlag(Object flag) {
		return findByProperty(FLAG, flag);
	}

	public List findAll() {
		log.debug("finding all Icreader instances");
		try {
			String queryString = "from Icreader";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}

	}

	public Icreader merge(Icreader detachedInstance) {
		log.debug("merging Icreader instance");
		try {
			Icreader result = (Icreader) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}

	}

	public void attachDirty(Icreader instance) {
		log.debug("attaching dirty Icreader instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Icreader instance) {
		log.debug("attaching clean Icreader instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}


Test.java
package gate.dao;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext; 

import java.util.List; 

import gate.model.Admin;
import gate.dao.AdminDAO;

public class Test {
    public static void main(String args[]) {
        ApplicationContext ctx = new FileSystemXmlApplicationContext(
                "src/applicationContext.xml");//载入Spring框架应用程序上下文中的信息
        AdminDAO test = (AdminDAO) ctx.getBean("AdminDAO");//获取DAO
        try { 
            List list=(List)(test.findAll());
            for(int i=0;i<list.size();i++){
            	Admin f=(Admin)(list.get(i));
                System.out.println("i:"+f.getName());
            }
        } catch (Exception e) { 
            System.err.println(e);
        }
    } 

}




applicationContext.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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
		<property name="url" value="jdbc:microsoft:sqlserver://xxx.xxx.xxx.xxx:1433" />
		<property name="username" value="xxxx" />
	</bean>
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="close">
		<property name="dataSource" ref="dataSource" />
		<property name="mappingResources">
			<list>
			<value>gate/model/Admin.hbm.xml</value>
			<value>gate/model/Icreader.hbm.xml</value>
			<value>gate/model/Loginlog.hbm.xml</value>
			<value>gate/model/Operator.hbm.xml</value>
			<value>gate/model/Oplog.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.SQLServerDialect
				</prop>
			</props>
		</property>
	</bean>

	<bean id="IcreaderDAO" class="gate.dao.IcreaderDAO">
       <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="OplogDAO" class="gate.dao.OplogDAO">
       <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="AdminDAO" class="gate.dao.AdminDAO">
       <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="OperatorDAO" class="gate.dao.OperatorDAO">
       <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="LoginlogDAO" class="gate.dao.LoginlogDAO">
       <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
</beans>


icreader-servlet.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
	<!-- 视图 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />	
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	

	
	<!-- 控制器 -->
    <bean id="listicreader" class="gate.web.ListIcreaderControllor" />
    <bean id="addicreader" class="gate.web.AddIcreaderControllor" >
    	<property name="sessionForm" value="true" />
    	<property name="commandName" value="icreaderform" />
    	<property name="commandClass" value="gate.web.model.IcreaderForm" />
    	<property name="formView" value="addicreader" />
    	<property name="successView" value="list" />
  
    </bean>
    <bean id="editicreader" class="gate.web.EditIcreaderControllor" >
    	<property name="sessionForm" value="true" />
    	<property name="commandName" value="icreaderform1" />
    	<property name="commandClass" value="gate.web.model.IcreaderForm" />
    	<property name="formView" value="editicreader" />
    	<property name="successView" value="list" />
    </bean>    
    <bean id="deleteicreader" class="gate.web.DeleteIcreaderControllor" />
    <!-- url映射 -->
	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings" > 
             <props> 
                 <prop key="/list">listicreader</prop> 
                 <prop key="/add">addicreader</prop>
                 <prop key="/edit">editicreader</prop> 
                 <prop key="/delete">deleteicreader</prop>
             </props> 
         </property> 
	</bean>
</beans>



ListIcreaderControllor
package gate.web;

import gate.service.IcreaderService;
import gate.service.VO.IcreaderVO;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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 ListIcreaderControllor implements Controller {

	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		IcreaderService c = new IcreaderService();
		Map<String, Object> myModel = new HashMap<String, Object>();
		List<IcreaderVO> l = c.findAllIcreader();
		myModel.put("icreaderlist", l);
		
		return new ModelAndView("list", "model", myModel);
	}

}


奇怪的是,在Test.java中测试findAll()就可以取到值,但在web下面访问就报NullPointerException异常.
16:11:05,921 ERROR IcreaderDAO:139 - find all failed
java.lang.NullPointerException
at gate.dao.IcreaderDAO.findAll(IcreaderDAO.java:137)
at gate.service.IcreaderService.findAllIcreader(IcreaderService.java:21)
at gate.web.ListIcreaderControllor.handleRequest(ListIcreaderControllor.java:22)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:833)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:639)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1285)
at java.lang.Thread.run(Unknown Source)
16:11:05,937 ERROR [icreader]:253 - Servlet.service() for servlet icreader threw exception
java.lang.NullPointerException
at gate.dao.IcreaderDAO.findAll(IcreaderDAO.java:137)
at gate.service.IcreaderService.findAllIcreader(IcreaderService.java:21)
at gate.web.ListIcreaderControllor.handleRequest(ListIcreaderControllor.java:22)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:833)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:639)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1285)
at java.lang.Thread.run(Unknown Source)
论坛首页 Java企业应用版

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