日期:2014-05-19  浏览次数:20523 次

SSH整合,NullPinterException
本人在做SSH整合时,然后想在页面把数据库里的数据显示出来。页面通过action获取数据,action代码为
Java code
package test.action;

import com.opensymphony.xwork2.ActionSupport;

import java.util.List;
import java.util.ArrayList;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import test.model.Admin;
import test.dao.impl.*;

public class ListAdmins extends ActionSupport {
    // private static BeanFactory factory = new
    // ClassPathXmlApplicationContext("applicationContext.xml");
    // private AdminDAOImpl adminDao=(AdminDAOImpl)factory.getBean("adminDao");
    private AdminDAOImpl adminDao;
    private List<Admin> list;

    public AdminDAOImpl getAdminDao() {
        return adminDao;
    }

    public void setAdminDao(AdminDAOImpl adminDao) {
        adminDao = adminDao;
    }

    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("step into execute");
        list = adminDao.queryAll();
        return super.SUCCESS;
    }
}


AdminDAOImpl代码为
Java code
package test.dao.impl;

import java.util.*;

import test.dao.*;
import test.model.Admin;

import org.apache.log4j.Logger;
//import org.codehaus.jettison.mapped.Configuration;
import org.hibernate.cfg.Configuration;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


public class AdminDAOImpl extends HibernateDaoSupport implements AdminDAO{
    public static final Logger logger=Logger.getLogger(AdminDAOImpl.class);
    public static List<Admin> adminList=new ArrayList<Admin>();
    
    public void save(Admin admin){
        logger.debug("save admin");
        try{
            getHibernateTemplate().save(admin);
            logger.debug("save admin successfullly");
        }catch(RuntimeException e){
            logger.error("save admin faily",e);
            throw e;
        }
        
    }
    
    public List<Admin> queryAll(){
        logger.debug("step into queryAll");
        String QUERY_ALL="from Admin ";
        adminList=getHibernateTemplate().getSessionFactory().openSession().createQuery(QUERY_ALL).list();
        //adminList=new Configuration().configure().buildSessionFactory().openSession().createQuery(QUERY_ALL).list();
        return adminList;
    }
}


application-context.xml如下:
XML code
<?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.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
    <ref bean="dataSource&