本人在做SSH整合时,然后想在页面把数据库里的数据显示出来。页面通过action获取数据,action代码为
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代码为
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 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>在ListAdmins这个action里我已经把adminDao注入进去了,但是运行程序时,一直报java.lang.NullPinterException,我debug时,发现adminDao为空,很诡异呀,谁来帮帮我!! 

解决方案 »

  1.   

    <bean id="listAdminsAction" class="test.action.ListAdmins">
    <property name="adminDao">
    <ref bean="adminDao" />
    </property>
    </bean>你的配置文件确实已经注入了,但是adminDao却为空,那么只有一种可能,你的action对象不是通过spring容器去获取的,通过反射或者直接new的,你看下你的struts与spring整合的是否有问题
      

  2.   

    楼主啊你真的好坑爹啊public void setAdminDao(AdminDAOImpl adminDao) {
            adminDao = adminDao;
        }我想请问一下这个方法能注入进来吗?你不觉得应该要这样写吗:
    public void setAdminDao(AdminDAOImpl adminDao) {
            this.adminDao = adminDao;
        }