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

SSH整合的错误,NullPointerException
SSH整合的一个小例子,struts2+Spring2+Hibernate3 
代码如下: 
Person.java 

Java code

package com.eva.po;
/**
 * Person entity.
 * 
 * @author eva 
 */
import java.io.Serializable;

public class Person implements Serializable{
    
    private int id;
    private String name;
    private int age;
    private String sex;
    private String address;
    
    /** defult constructor */
    public Person() {
        
    }
    /** minimal constructor */
    public Person(String name) {
        this.name = name;
    }
    /** full constructor */
    public Person(String name,int age,String sex,String address) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.address = address;
    }
         //setter and getter
}



PersonDAO.java 
Java code
 
package com.eva.dao;   
  
import java.util.List;   
  
import com.eva.po.Person;   
  
public interface PersonDAO {   
    public void saveOrUpdate(Person person);   
    public void update(Person person);   
    public void delete(Person person);   
    public Person get(String id);   
    public List<Person> findAll();   
}  




PersonHibernateDAO.java 
Java code

package com.eva.dao.hibernate;   
  
import java.util.List;   
  
import com.eva.dao.PersonDAO;   
import com.eva.po.Person;   
  
import org.hibernate.SessionFactory;   
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;   
  
public class PersonHibernateDAO extends HibernateDaoSupport implements  
        PersonDAO {   
    public void delete(Person person) {   
        this.getHibernateTemplate().delete(person);   
    }   
  
    public List<Person> findAll() {   
        List<Person> persons = this.getHibernateTemplate().find("from Person");   
        return persons;   
    }   
  
    public Person get(String id) {   
        Person person = (Person)this.getHibernateTemplate().get(Person.class, id);   
        return person;   
    }   
  
    public void saveOrUpdate(Person person) {   
        this.getHibernateTemplate().saveOrUpdate(person);   
    }   
  
    public void update(Person person) {   
        this.getHibernateTemplate().saveOrUpdate(person);   
    }   
  
}  



PersonService.java 
Java code

package com.eva.service;   
  
import java.util.List;   
  
import com.eva.dao.hibernate.PersonHibernateDAO;   
import com.eva.po.Person;   
  
public class PersonService {   
    private PersonHibernateDAO personHibernateDAO;   
       
    public void setPersonHibernateDAO(PersonHibernateDAO personHibernateDAO) {   
        this.personHibernateDAO = personHibernateDAO;   
        System.out.println("personHibernateDAO is null ? --- " + personHibernateDAO == null);   
    }   
       
    public PersonHibernateDAO getPersonHibernateDAO() {   
        return personHibernateDAO;   
    }   
  
    public void saveOrUpdate(Person person)   
    {   
        personHibernateDAO.saveOrUpdate(person);   
    }   
  
    public void update(Person person)   
    {   
        personHibernateDAO.update(person);   
    }   
  
    public void delete(Person person)   
    {   
        personHibernateDAO.delete(person