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.javapackage 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)请各位高手帮忙解决,谢谢了。

解决方案 »

  1.   

    给你一个demo http://download.csdn.net/detail/s478853630/4382009
    或许能帮你
      

  2.   

    第一种,用注解,自动装配:
    @Autowire
    private LoginService loginService ;
    private UserinfoDAO userInfoDAO1 ;
    第二种,从context种获取:private LoginService loginService;
    public void execute() {
       loginService = ApplicationContext.getBean("loginService");
    }
      

  3.   

    s478853630你好,现在我的csdn账号积分不够,这个demo下不下来,你能发到我邮箱吗?小弟非常感谢!qq邮箱:3200185980
      

  4.   

    s478853630 你好,我已经上传了一个 飞秋,你若能帮小弟刷分,小弟真的万分感激!谢谢了。其实有qq邮箱是可以发的,qq邮箱好些支持2g大小。
      

  5.   

    按照你的配置我建立了个工程,并没有出问题
    main方法:
    ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");    
    UserAction mUserAction = (UserAction)factory.getBean("userAction1");
    mUserAction.execute();你的action:
    public String execute() throws Exception {
    System.out.println("11");
    // loginService.getUserInfoList();
    userInfoDAO1.findAll();
    System.out.println("22");
    return SUCCESS;
    }打印,输出:
    2012-11-18 12:59:05 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1301ed8: startup date [Sun Nov 18 12:59:05 CST 2012]; root of context hierarchy
    2012-11-18 12:59:06 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [applicationContext.xml]
    2012-11-18 12:59:07 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1f64158: defining beans [dataSource,sessionFactory,userinfoDAO,userAction1]; root of factory hierarchy
    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
    2012-11-18 12:59:09 org.springframework.orm.hibernate3.LocalSessionFactoryBean buildSessionFactory
    信息: Building new Hibernate SessionFactory
    11
    22
      

  6.   


    你好,我直接用浏览器打开调用userAction不知道为什么报空指针异常,小弟刚接触ssh,麻烦你帮忙看看,谢谢了。
      

  7.   

    这个问题 怎么发了两个贴 ?多去看看异常,分析下 就会明白了。。我在你javaee版中已经回复了。
      

  8.   


    你好 ,我改完后还是一样的,(小弟刚接触ssh,烦请帮忙解决下)
    你是说这个里面配置错了吗?应该怎么配置?之前:
    <bean id="userAction1" class="com.ffms.action.UserAction">
    <property name="userInfoDAO1" ref="userinfoDAO"></property>
    </bean>
      

  9.   

    <property name="userInfoDAO1" ref="userinfoDAO"></property>
    命名很不规范,看着看类,字母有时候大写有时候小写,,  
    这么配置很误导人,如果装配方式使用类型装配就行不通了,但是我没有看到。。
    还是规范点的好。
    另外 Action中直接注入dao不好,不符合dao模式规范。userInfoDAO1为空 ,你把代码改规范之后在看看。。