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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></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>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/ffms/hbm/Userinfo.hbm.xml</value></list>
</property></bean>
<bean id="userinfoDAO" class="com.ffms.dao.UserinfoDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<!--<bean id="loginService" class="com.ffms.service.LoginService">
<property name="uifd" ref="UserinfoDAO"></property>
</bean>
-->
<bean id="userAction1" class="com.ffms.action.UserAction">
<property name="userInfoDAO1" ref="userinfoDAO"></property>
</bean>
</beans>
UserAction.javapackage com.ffms.action;import com.ffms.dao.UserinfoDAO;
import com.ffms.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {
private LoginService loginService ;
private UserinfoDAO userInfoDAO1 ;

public String execute() throws Exception {
System.out.println("11");
//loginService.getUserInfoList();
userInfoDAO1.findAll();
return SUCCESS;
} public void setLoginService(LoginService loginService) {
this.loginService = loginService;
} public LoginService getLoginService() {
return loginService;
} public UserinfoDAO getUserInfoDAO1() {
return userInfoDAO1;
} public void setUserInfoDAO1(UserinfoDAO userInfoDAO1) {
this.userInfoDAO1 = userInfoDAO1;
}
}
UserinfoDAO.java
package com.ffms.dao;import java.util.List;import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.ffms.pojo.Userinfo;/**
  * A data access object (DAO) providing persistence and search support for Userinfo entities.
  * Transaction control of the save(), update() and delete() operations 
can directly support Spring container-managed transactions or they can be augmented to handle user-managed Spring transactions. 
Each of these methods provides additional information for how to configure it for the desired type of transaction control. 
 * @see com.ffms.pojo.Userinfo
  * @author MyEclipse Persistence Tools 
 */public class UserinfoDAO extends HibernateDaoSupport  {
     private static final Logger log = LoggerFactory.getLogger(UserinfoDAO.class);
//property constants
public static final String USER_NAME = "userName";
public static final String PASS_WORD = "passWord"; protected void initDao() {
//do nothing
}
    
    public void save(Userinfo transientInstance) {
        log.debug("saving Userinfo instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
public void delete(Userinfo persistentInstance) {
        log.debug("deleting Userinfo instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public Userinfo findById( java.lang.Integer id) {
        log.debug("getting Userinfo instance with id: " + id);
        try {
            Userinfo instance = (Userinfo) getHibernateTemplate()
                    .get("com.ffms.pojo.Userinfo", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(Userinfo instance) {
        log.debug("finding Userinfo 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 Userinfo instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Userinfo 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 findByUserName(Object userName
) {
return findByProperty(USER_NAME, userName
);
}

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

    public Userinfo merge(Userinfo detachedInstance) {
        log.debug("merging Userinfo instance");
        try {
            Userinfo result = (Userinfo) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }    public void attachDirty(Userinfo instance) {
        log.debug("attaching dirty Userinfo instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Userinfo instance) {
        log.debug("attaching clean Userinfo instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    } public static UserinfoDAO getFromApplicationContext(ApplicationContext ctx) {
     return (UserinfoDAO) ctx.getBean("UserinfoDAO");
}
public static void main(String[] args) {
UserinfoDAO udao = new UserinfoDAO();

List<Userinfo> list = udao.findAll();
for (int i = 0; i <list.size(); i++) {
System.out.println(list.get(i));
}

}
}
报错
HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
com.ffms.action.UserAction.execute(UserAction.java:14)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)

解决方案 »

  1.   

    UserAction
    import com.ffms.dao.UserinfoDAO;
    import com.ffms.service.LoginService;
    private LoginService loginService ;
    private UserinfoDAO userInfoDAO1 ;面向接口编程~~~LZ换成接口吧、不要直接用实现类!
    配置的时候用实现类....代码中用接口!
      

  2.   

     userInfoDAO1.findAll(); 是这里面有问题;
      

  3.   

    你好,这个是hibernate里面自带的方法呀。
      

  4.   

    userInfoDAO1为空,导致 NullPointerException,  我看了下 applicationContext.xml 中没有注入userInfoDAO1,只有userInfoDAO  ,所以这就是根本原因。。
      

  5.   

    你看下jar包有没有重复,要仔细点,还有这种编程模式要改变下。
      

  6.   

    ACTION 中属性userInfoDAO1,而applicationContext.xml 中注入了userInfoDAO,所以NullPointerException。
      

  7.   

    我觉得非常有必要检查一下你的sessionFactory
      

  8.   

    感谢热心人的回答,我找到问题了,是我少导了一个struts2-spring的jar包。