当前位置: 代码迷 >> J2EE >> SSH调整,NullPinterException
  详细解决方案

SSH调整,NullPinterException

热度:39   发布时间:2016-04-22 00:59:31.0
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" /></property><property name="hibernateProperties">    <props>    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>    <prop key="hibernate.show_sql">true</prop>    </props></property><property name="mappingResources"><list><value>Admin.hbm.xml</value></list></property></bean><bean id="adminDao" class="test.dao.impl.AdminDAOImpl"><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean><bean id="loginAction" class="test.action.Login"><property name="adminDao"><ref bean="adminDao" /></property></bean><bean id="listAdminsAction" class="test.action.ListAdmins"><property name="adminDao"><ref bean="adminDao" /></property></bean><!--  bean id="myBean" class="test.model.MyBean" >    <property name="id">        <value>1</value>    </property>    <property name="name">        <value>nueton</value>    </property></bean--></beans>
  相关解决方案