SSH整合问题 NullPointerException

SSH整合的一个小例子,struts2+Spring2+Hibernate3
代码如下:
Person.java
[code="java"]
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

}
[/code]

PersonDAO.java
[code="java"]
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 findAll();
}
[/code]

PersonHibernateDAO.java
[code="java"]
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);
}

}
[/code]

PersonService.java
[code="java"]
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);
}

public List<Person> findAll()
{
    return personHibernateDAO.findAll();
}

public Person get(String id)
{
    return personHibernateDAO.get(id);
}

}

[/code]

LoginAction.java
[code="java"]
package com.eva.action;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.eva.po.Person;
import com.eva.service.PersonService;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
private PersonService personService;
private Person person;
private String id;
private List persons;

public LoginAction() {

}

public Person getPerson() {
    return person;
}

public void setPerson(Person person) {
    this.person = person;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public PersonService getPersonService() {
    return personService;
}

public void setPersonService(PersonService personService) {
    this.personService = personService;
}

public List<Person> getPersons() {
    return persons;
}

public void setPersons(List<Person> persons) {
    this.persons = persons;
}

public String execute() throws Exception {
    if (person != null & !person.equals("")) {
        personService.saveOrUpdate(person);
    }
    persons = personService.findAll();
    setPersons(persons);

    System.out.println(persons.size());
    return SUCCESS;
}

public String list() throws Exception {
    persons = personService.findAll();
    setPersons(persons);
    return SUCCESS;
}

public String save() throws Exception {;
    personService.saveOrUpdate(person);
    return SUCCESS;
}

public String update() throws Exception {
    person = personService.get(id);
    personService.update(person);
    return SUCCESS;
}

public String getValue() throws Exception {
    return SUCCESS;
}
public String delete() throws Exception{
    person = personService.get(id);
    personService.delete(person);
    return SUCCESS;
}

}
[/code]

applicationContext.xml

[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<!-- dataSource配置 -->
<bean id="dataSource"  
    class="org.apache.commons.dbcp.BasicDataSource">  
    <property name="driverClassName">
        <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    </property>
    <property name="url">
        <value>
            jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ssh 
        </value>
    </property>
    <property name="username">
        <value>sa</value>
    </property>
    <property name="password">
        <value>sa</value>
    </property>
</bean> 
<!-- 配置session工厂 -->

<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.SQLServerDialect    
            </prop>  
        </props>  
    </property>  
    <property name="mappingResources">  
        <list>  
            <value>com/eva/po/Person.hbm.xml</value></list>  
    </property> 
</bean>

<!-- 具体的数据库访问类 -->
<bean id="dao" class="com.eva.dao.hibernate.PersonHibernateDAO">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property>
</bean>
<!-- 配置具体的业务逻辑类 -->
<bean id="personService" class="com.eva.service.PersonService">
    <property name="personHibernateDAO">
        <ref bean="dao"/>
    </property>
</bean>

<!-- 配置action -->
<bean id="loginAction" class="com.eva.action.LoginAction">
    <property name="personService">
        <ref bean="personService"/>
    </property>
</bean>


[/code]

struts.xml
[code="java"]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">



<!-- 一定要加,对象工厂是spring -->

<!-- 开启开发模式 -->

<package name="login" extends="struts-default">
    <!-- 保存 -->
    <action name="Login" method="save" class="com.eva.action.LoginAction">
        <result>success.jsp</result>
        <!-- <result name="error">error.jsp</result> -->
    </action>
    <!-- 显示出所有列表 -->
    <action name="list" method="list" class="com.eva.action.LoginAction">
        <result>success.jsp</result>
    </action>
    <!-- 删除 -->
    <action name="delete" method="delete" class="com.eva.action.LoginAction">
        <result>success.jsp</result>
    </action>
    <!-- 更新 -->
    <action name="update" method="update" class="com.eva.action.LoginAction">
        <result>index.jsp</result>
    </action>
</package>


[/code]

Person.hbm.xml
[code="java"]
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">





















[/code]

web.xml
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
person

struts2

org.apache.struts2.dispatcher.FilterDispatcher


classes
java.lang.Object

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/applicationContext*.xml</param-value> 
</context-param> 
<!-- 添加监听器 -->
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>


[/code]

index.jsp
[code="java"]
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


查看全部
yi_liu
yi_liu
2010/07/07 16:46
  • spring
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

0个回复