SSH搭建 Spring无法注入的问题 请大神们赐教


在此处挣扎了许久,终于还是要到这里问大神们来了....
在运行add.jsp,提交表单后出现以下错误提示:
java.lang.NullPointerException
com.dip.pm.dao.Hibernate.PersonDAOHibernate.add(PersonDAOHibernate.java:18)
com.dip.pm.service.impl.PersonManagerImpl.addPerson(PersonManagerImpl.java:21)
com.dip.pm.action.PersonAction.add(PersonAction.java:19)
该加的包都加了,使用的是spring 2.0 + struts 2.1 + hibernate 3.1,出现这个问题肯定是由于注入的bean为null造成的,而且主要原因在PersonManagerImpl.java中,无法实现PersonDAOHibernate.java的注入,原因不详,请大神们指点一二.....感激不尽.....

package com.dip.pm.dao.Hibernate;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.orm.toplink.SessionFactory;

import com.dip.pm.dao.PersonDAO;
import com.dip.pm.model.Person;

public class PersonDAOHibernate extends HibernateDaoSupport implements
PersonDAO {

public void add(Person person) {
    // TODO Auto-generated method stub
    System.out.println("hi5");
    System.out.println(person.toString());
    getHibernateTemplate().save(person);
    System.out.println("hi6");

}

public void delete(String id) {
    // TODO Auto-generated method stub
    this.getHibernateTemplate().delete(getPerson(id));
}

public Person getPerson(String id) {
    // TODO Auto-generated method stub
    return (Person)this.getHibernateTemplate().get(Person.class, id);
}

public List getPersons() {
    // TODO Auto-generated method stub
    return getHibernateTemplate().find("from Person");
}

public void update(Person person) {
    // TODO Auto-generated method stub
    getHibernateTemplate().update(person);
}

public PersonDAOHibernate(){
    System.out.println("Come on!");
}

}




package com.dip.pm.service.impl;

import java.util.List;

import com.dip.pm.dao.PersonDAO;
import com.dip.pm.model.Person;
import com.dip.pm.service.PersonManager;

public class PersonManagerImpl implements PersonManager {

private PersonDAO personDAO;

public Person Person(String id) {
    // TODO Auto-generated method stub
    return personDAO.getPerson(id);
}

public PersonManagerImpl(){
    System.out.println("wo cao");
}
public void addPerson(Person person) {
    // TODO Auto-generated method stub
    System.out.println("hi3");
    personDAO.add(person);
    System.out.println("hi4");
}

public void delPerson(String id) {
    // TODO Auto-generated method stub
    personDAO.delete(id);
}

public void editPerson(Person person) {
    // TODO Auto-generated method stub
    personDAO.update(person);
}

public List listPerson() {
    // TODO Auto-generated method stub
    return null;
}

public void setPersonDAO(PersonDAO personDAO) {
    this.personDAO = personDAO;
}

public PersonDAO getPersonDAO() {
    return personDAO;
}

}



package com.dip.pm.action;

import java.util.List;

import org.springframework.context.ApplicationContext;

import com.dip.pm.model.Person;
import com.dip.pm.service.PersonManager;
import com.opensymphony.xwork2.ActionSupport;

public class PersonAction extends ActionSupport{

private PersonManager personManager;
private Person person;


public String add(){
    System.out.println("hi1");
    personManager.addPerson(person);
    System.out.println("hi2");
    return SUCCESS;
}

public Person get(){
    return personManager.Person(person.getId());
}

public List list(){
    return personManager.listPerson();
}

public void delete(){
    personManager.delPerson(person.getId());
}

public void upadate(){
    personManager.editPerson(person);
}

public void setPersonManager(PersonManager personManager) {
    this.personManager = personManager ;
}

}



<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%@ taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<s:form action="add">

    &lt;s:textfield name="person.name" label="name"/&gt;
    &lt;s:textfield name="person.password" label="password"/&gt;
    &lt;s:textfield name="person.sex" label="sex"/&gt;
    &lt;s:textfield name="person.email" label="email"/&gt;
    &lt;s:submit/&gt;

</s:form>
</body>
</html>



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>
<param-name>contexConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>

&lt;filter&gt;  
    &lt;filter-name&gt;lazyLoadingFilter&lt;/filter-name&gt;    
    &lt;filter-class&gt;    
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    &lt;/filter-class&gt;    
&lt;/filter&gt;    

&lt;filter-mapping&gt;    
    &lt;filter-name&gt;lazyLoadingFilter&lt;/filter-name&gt;    
    &lt;url-pattern&gt;*.action&lt;/url-pattern&gt;    
&lt;/filter-mapping&gt;

&lt;listener&gt;
    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
&lt;/listener&gt;

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

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

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

</web-app>




<?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">
<struts>
<constant name="struts.objectFactory" value="spring"/>
<package name="crm_person" extends="struts-default" namespace="/">
<action name="add" class="addBean" method="add">
<result name="success">/hello.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>


<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
&lt;bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource"&gt;
    &lt;property name="driverClassName"
        value=" org.gjt.mm.mysql.Driver"&gt;
    &lt;/property&gt;
    &lt;property name="url"
        value="jdbc:mysql://localhost:3306/test"&gt;
    &lt;/property&gt;
    &lt;property name="username" value="root"&gt;&lt;/property&gt;
    &lt;property name="password" value="root"&gt;&lt;/property&gt;
&lt;/bean&gt;

&lt;bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
    &lt;property name="dataSource"&gt;
        &lt;ref bean="dataSource"/&gt;
    &lt;/property&gt;
    &lt;property name="mappingResources"&gt;
        &lt;list&gt;
            &lt;value&gt;com/dip/pm/model/Person.hbm.xml&lt;/value&gt;
        &lt;/list&gt;
    &lt;/property&gt;
    &lt;property name="hibernateProperties"&gt;
        &lt;props&gt;
            &lt;prop key="hibernate.dialect"&gt;
                org.hibernate.dialect.MySQLDialect
            &lt;/prop&gt;
            &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;  
        &lt;/props&gt;
    &lt;/property&gt;
&lt;/bean&gt;
&lt;bean id="personDAO"
    class="com.dip.pm.dao.Hibernate.PersonDAOHibernate"&gt;
    &lt;property name="sessionFactory"&gt;
        &lt;ref local="sessionFactory" /&gt;
    &lt;/property&gt;
&lt;/bean&gt;
&lt;bean id="personManager"
    class="com.dip.pm.service.impl.PersonManagerImpl"&gt;
    &lt;property name="personDAO"&gt;
        &lt;ref bean="personDAO" /&gt;
    &lt;/property&gt;
&lt;/bean&gt;
&lt;bean id="addBean" class="com.dip.pm.action.PersonAction"
    scope="prototype"&gt;
    &lt;property name="personManager"&gt;
        &lt;ref bean="personManager" /&gt;
    &lt;/property&gt;
&lt;/bean&gt;

&lt;!-- 配置事务管理器 

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>-->

<!-- 配置事务的传播特性
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice> -->

<!-- 那些类的哪些方法参与事务
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* com.bcm.service.impl.*.*(..))"/>
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>-->

</beans>

3个回答

[code="java"]public class PersonAction extends ActionSupport{

private PersonManager personManager;   
private Person person;   

...
}
[/code]
person 没有setter及getter方法。

添加如下代码:[code="java"]
public Person getPerson(){
return this.person;
}
public void setPerson(Person person){
this.person = person;
}

[/code]

根据你的出错提示[quote]java.lang.NullPointerException
com.dip.pm.dao.Hibernate.PersonDAOHibernate.add(PersonDAOHibernate.java:18)
com.dip.pm.service.impl.PersonManagerImpl.addPerson(PersonManagerImpl.java:21)
com.dip.pm.action.PersonAction.add(PersonAction.java:19)
[/quote]
应该是PersonDAOHibernate.java 18行,如果你贴的代码没有问题的话,应该是你传入的Person为null

你用的是struts2,action中没有get和set方法

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问