ApplicationContext.xml
<beans
xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/db_database07"></property>
<property name="username" value="root"></property>
<property name="password" value="111"></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/lwy/dto/Userlogin.hbm.xml</value>
<value>com/lwy/dto/Specialty.hbm.xml</value>
<value>com/lwy/dto/Stuuser.hbm.xml</value>
<value>com/lwy/dto/Course.hbm.xml</value>
<value>com/lwy/dto/Stucourse.hbm.xml</value></list>
</property></bean>
<!-- 定义事务管理器 -->


<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!--定义注释驱动-->
<tx:annotation-driven transaction-manager="transactionManager" /> <!-- -->
    <bean id="userLoginDao" class="com.lwy.dao.UserLoginDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="specialtyDao" class="com.lwy.dao.SpecialtyDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="stuUserDao" class="com.lwy.dao.StuUserDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="courseDao" class="com.lwy.dao.CourseDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
    <bean name="/userLogin" class="com.lwy.action.UserLoginAction">
<property name="userLoginDao" ref="userLoginDao" />
<property name="stuUserDao" ref="stuUserDao" />
<property name="specialtyDao" ref="specialtyDao" />
</bean>
<bean name="/reg" class="com.lwy.action.RegAction">
      <property name="userLoginDao" ref="userLoginDao"></property>
      <property name="specialtyDao" ref="specialtyDao"></property>
</bean>
    <bean name="/stuUser" class="com.lwy.action.StuUserAction">
<property name="stuUserDao" ref="stuUserDao" />
<property name="courseDao" ref="courseDao" />
<property name="specialtyDao" ref="specialtyDao" />
</bean>
<bean name="/specialty" class="com.lwy.action.SpecialtyAction">
      <property name="specialtyDao" ref="specialtyDao"></property>
</bean>
    <bean name="/course" class="com.lwy.action.CourseAction">
           <property name="courseDao" ref="courseDao"></property>
           <property name="specialtyDao" ref="specialtyDao"></property>
   </bean>

<bean name="/statInfo" class="com.lwy.action.StatInfoAction">
       <property name="courseDao" ref="courseDao"></property>
       <property name="specialtyDao" ref="specialtyDao"></property>
</bean>
<bean name="/updatePwd" class="com.lwy.action.UpdatePwdAction">
        <property name="userLoginDao" ref="userLoginDao"></property>
</bean>
</beans>
UserLoginAction:
public class UserLoginAction extends Action {
private ISpecialtyDao specialtyDao;
private IStuUserDao stuUserDao;
private IUserLoginDao userLoginDao;
/*
 * Generated Methods
 */
public void setSpecialtyDao(ISpecialtyDao specialtyDao) {
this.specialtyDao = specialtyDao;
} public void setStuUserDao(IStuUserDao stuUserDao) {
this.stuUserDao = stuUserDao;
} public void setUserLoginDao(IUserLoginDao userLoginDao) {
this.userLoginDao = userLoginDao;
}
/** 
 * Method execute
 * @param mapping
 * @param form
 * @param request
 * @param response
 * @return ActionForward
 */
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DynaActionForm userLoginForm = (DynaActionForm) form;// TODO Auto-generated method stub
ApplicationContext ac = new ClassPathXmlApplicationContext("../applicationContext.xml");
IUserLoginDao dao = (IUserLoginDao)ac.getBean("userLoginDao");
System.out.println(" dao --> "+ dao);
Userlogin userLogin=userLoginDao.findbyNameAndPwd(userLoginForm.getString("loginName"),            userLoginForm.getString("pwd"));
if(userLogin==null){
request.setAttribute("error", "用户登录失败,用户名或密码不正确");
return mapping.findForward("index");
}
else{
request.getSession().setAttribute("loginName", userLogin.getLoginName());
request.getSession().setAttribute("id",userLogin.getId());
if(userLogin.getType().equals("1")){
System.out.println("管理员登录");
return mapping.findForward("manager");

}
else{
Stuuser stuUser=stuUserDao.findById(userLogin.getId());
if(stuUser==null){
System.out.println("未填写基本信息");
List<Specialty> list=specialtyDao.findStuByAll();
request.setAttribute("list", list);
return mapping.findForward("addStuInfo");
}

else{
Specialty specialty=specialtyDao.findById(stuUser.getSpecialtyId());
request.setAttribute("specialty", specialty);
request.setAttribute("stuUser", stuUser);
System.out.println("填写过基本信息");
return mapping.findForward("welcome");

}
  }      }
   }
}
经过测试,在UserLoginAction中调用UserLoginDao时,UserLoginDao=null。通过ApplicationContext中获取bean,ApplicationContext ac = new ClassPathXmlApplicationContext("../applicationContext.xml");
IUserLoginDao dao = (IUserLoginDao)ac.getBean("userLoginDao");
这样可以获取dao,应该是注入问题,但是本人是在看不出哪里有问题,纠结好久了,求大神帮忙

解决方案 »

  1.   

    大哥我也在弄ssh  qq:790318448  交流下啊
      

  2.   

    IUserLoginDao检查哈get,set方法是否写了
      

  3.   

    private IUserLoginDao userLoginDao;
    private IStuUserDao stuUserDao;
    private ISpecialtyDao specialtyDao; public void setSpecialtyDao(ISpecialtyDao specialtyDao) {
    this.specialtyDao = specialtyDao;
    } public void setStuUserDao(IStuUserDao stuUserDao) {
    this.stuUserDao = stuUserDao;
    } public void setUserLoginDao(IUserLoginDao userLoginDao) {
    this.userLoginDao = userLoginDao;
    }
    set方法已经写了
      

  4.   

    大哥- -IUserLoginDao是个接口,只能申明方法,不能实现具体的方法,set方法是写在action中的
      

  5.   

    哦,那你就检查IUserLoginDao的实现类。
    ApplicationContext ac = new ClassPathXmlApplicationContext("../applicationContext.xml");
    IUserLoginDao dao = (IUserLoginDao)ac.getBean("userLoginDao");
    System.out.println(" dao --> "+ dao);
    这后面打印dao里有返回值的一个方法,看有值不。
      

  6.   

    这样获取是有值的,所以我判断应该是dao注入到action的时候出错了,但是我实在是看不出来啊
      

  7.   

    Userlogin userLogin=userLoginDao.findbyNameAndPwd(userLoginForm.getString("loginName"), userLoginForm.getString("pwd"));
    这有问题吧,userLoginDao 应该是dao吧,你写错了吧
      

  8.   

    没错的dao是我手动从ApplicationContext里获取的,用来测试这个dao存不存在,答案是存在的。userLoginDao是通过注入获取的,通过注入获取就给我空指针错误,说明注入不对,具体怎么不对,求解啊
      

  9.   

    哦,你用dao调用后面的方法走下去,如果行的通的话,估计就是配置的问题吧,如果行不通,你就检查后面写的是否正确了,不过,我占时没看出啥问题,呵呵
      

  10.   

    IUserLoginDao dao = (IUserLoginDao)ac.getBean("userLoginDao");
    System.out.println(" dao --> "+ dao);
    Userlogin userLogin=userLoginDao.findbyNameAndPwd(userLoginForm.getString("loginName"), userLoginForm.getString("pwd"));
    这里,你上面得到是变量名称是dao,为什么你下面调用的时候使用userLoginDao这个变量,你说
    ApplicationContext ac = new ClassPathXmlApplicationContext("../applicationContext.xml");
    IUserLoginDao dao = (IUserLoginDao)ac.getBean("userLoginDao");
    这样是可以获取dao的,那你觉得是哪里出了问题?我觉得,你这个userLoginDao.findbyNameAndPwd调用,来的莫名其妙,不知道是否是你有别的意思、
      

  11.   

    我知道你哪错了,你这里的配置错了啊
      <bean id="userLoginDao" class="com.lwy.dao.UserLoginDao">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    应改为:
      <bean id="userLoginDao" class="com.lwy.dao.IUserLoginDao">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
      

  12.   

    ApplicationContext ac = new ClassPathXmlApplicationContext("../applicationContext.xml");
    IUserLoginDao dao = (IUserLoginDao)ac.getBean("userLoginDao");
    这个只是用来验证ApplicationContext.xml中的<bean id=userLoginDao>是否正确,这段代码本身是不存在的。
    而这段代码Userlogin userLogin=userLoginDao.findbyNameAndPwd(userLoginForm.getString("loginName"), userLoginForm.getString("pwd"));才是真正通过注入来获取dao的
      

  13.   

    我知道。 
    <bean id="userLoginDao" class="com.lwy.dao.UserLoginDao">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    应改为:
      <bean id="userLoginDao" class="com.lwy.dao.IUserLoginDao">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    就是这里出错了啊,你没改,试试啊
      

  14.   

    不是,这个class指向的就是userLoginDao的包+文件名称
      

  15.   

    UserLoginDao不是IUserLoginDao的实现类?如果是,肯定就是这的错误了,你不相信,改着试试
      

  16.   

    这是dao
     <bean id="userLoginDao" class="com.lwy.dao.UserLoginDao">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    这是biz 
    <bean id="IDao" class="com.lwy.dao.IUserLoginDao">
    <property name="userLoginDao" ref="userLoginDao" />
    </bean>
    然后你在配你的Action啊
      

  17.   

    ...教程里不是这么教的啊- -
    你说的我也试了,直接叉叉No setter found for property 'userLoginDao' in class 'com.lwy.dao.IUserLoginDao'
      

  18.   

    我真是说不清了啊,反正你配置要使UserLoginDao和IUserLoginDao联系起来才行
      

  19.   

    我晕,搞了这么多,没搞出来,失败啊,要不你把QQ给我,我们QQ上在说哈
      

  20.   

    userLoginDao 你确定 你这个dao里面 有sessionFactory这个属性? 如果有 你确定sessionFactory这个值是否为空?
      

  21.   

    问题就在于你的dao里面是否注入了sessionFactory
    @Repository("utilsDao")
    public class UtilsDaoImpl extends HibernateDaoSupport implements IUtilsDao { @Resource(name="sessionFactory")
    public void setSuperSessionFactory(SessionFactory sessionFactory) {
    super.setSessionFactory(sessionFactory);
    }
      

  22.   

    我确定有sessionFactory,并且不为空,因为直接取出dao能用,说明sessionFactory已经注入了
      

  23.   

    我确定有sessionFactory,并且不为空,因为直接取出dao能用,说明sessionFactory已经注入了
      

  24.   

    有没有加入strut-spring这个插件包
      

  25.   

    晕,原来是struts1.x啊
    这要看看你的struts-config.xml
      

  26.   

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
      <data-sources />
      <form-beans>
        <form-bean name="userLoginForm" type="org.apache.struts.action.DynaActionForm">
          <form-property name="id" type="java.lang.Integer" />
          <form-property name="mail" type="java.lang.String" />
          <form-property name="pwd" type="java.lang.String" />
          <form-property name="type" type="java.lang.String" />
          <form-property name="pwd1" type="java.lang.String" />
          <form-property name="loginName" type="java.lang.String" />
        </form-bean>
        <form-bean name="StuUserForm" type="org.apache.struts.action.DynaActionForm">
          <form-property name="id" type="java.lang.Integer" />
          <form-property name="stuNo" type="java.lang.String" />
          <form-property name="tel" type="java.lang.String" />
          <form-property name="homeAddr" type="java.lang.String" />
          <form-property name="birthday" type="java.lang.String" />
          <form-property name="specialtyId" type="java.lang.Integer" />
          <form-property name="addr" type="java.lang.String" />
          <form-property name="stuName" type="java.lang.String" />
          <form-property name="stuSex" type="java.lang.String" />
        </form-bean>
        <form-bean name="specialtyForm" type="org.apache.struts.action.DynaActionForm">
          <form-property name="id" type="java.lang.Integer" />
          <form-property name="name" type="java.lang.String" />
          <form-property name="isFinish" type="java.lang.Boolean" />
          <form-property name="enterYear" type="java.lang.String" />
          <form-property name="langthYear" type="java.lang.String" />
        </form-bean>
        <form-bean name="courseForm" type="org.apache.struts.action.DynaActionForm">
          <form-property name="id" type="java.lang.Integer" />
          <form-property name="name" type="java.lang.String" />
          <form-property name="isFinish" type="java.lang.Boolean" />
          <form-property name="courseInfo" type="java.lang.String" />
          <form-property name="specialtyId" type="java.lang.Integer" />
          <form-property name="teacherInfo" type="java.lang.String" />
          <form-property name="addr" type="java.lang.String" />
          <form-property name="credit" type="java.lang.Short" />
          <form-property name="teacherName" type="java.lang.String" />
          <form-property name="schooltime" type="java.lang.String" />
        </form-bean>
      </form-beans>
      <global-exceptions />
      <global-forwards />
      <action-mappings>
        <action
          attribute="userLoginForm"
          name="userLoginForm"
          path="/reg"
          scope="request"
          type="org.springframework.web.struts.DelegatingActionProxy">
          <set-property property="cancellable" value="true" />
          <forward name="addStuInfo" path="/stu/addStuInfo.jsp" />
          <forward name="reg" path="/reg.jsp" />
        </action>
        <action
          attribute="StuUserForm"
          name="StuUserForm"
          parameter="method"
          path="/stuUser"
          scope="request"
          type="org.springframework.web.struts.DelegatingActionProxy">
          <set-property property="cancellable" value="true" />
          <forward name="course" path="/stu/courseInfo.jsp" />
          <forward name="selected" path="/stu/selected.jsp" />
          <forward name="select" path="/stu/select.jsp" />
          <forward name="welcome" path="/stu/welcome.jsp" />
          <forward name="exit" path="index.jsp" />
        </action>
        <action
          attribute="userLoginForm"
          name="userLoginForm"
          path="/userLogin"
          scope="request"
          type="com.lwy.action.UserLoginAction">
          <set-property property="cancellable" value="true" />
          <forward name="manager " path="/manager/manager.jsp" />
          <forward name="index" path="/index.jsp" />
          <forward name="addStuInfo" path="/stu/addStuInfo.jsp" />
          <forward name="welcome" path="/stu/welcome" />
        </action>
        <action
          attribute="specialtyForm"
          name="specialtyForm"
          path="/specialty"
          scope="request"
          type="com.lwy.action.SpecialtyAction">
          <set-property property="cancellable" value="true" />
          <forward name="showSpecialty" path="/manager/showSpecialty.jsp" />
        </action>
        <action
          attribute="courseForm"
          name="courseForm"
          path="/course"
          scope="request"
          type="com.lwy.action.CourseAction">
          <set-property property="cancellable" value="true" />
          <forward name="addCourse " path="/manager/addCourse.jsp" />
          <forward name="courseInfo" path="/manager/courseInfo.jsp" />
          <forward name="shouCourse" path="/manager/showCourse.jsp" />
        </action>
        <action
          attribute="courseForm"
          name="courseForm"
          path="/statInfo"
          scope="request"
          type="com.lwy.action.StatInfoAction">
          <set-property property="cancellable" value="true" />
          <forward name="showStat" path="/manager/showStat.jsp" />
          <forward name="stuList" path="/manager/stuList.jsp" />
        </action>
        <action
          attribute="userLoginForm"
          name="userLoginForm"
          path="/updatePwd"
          scope="request"
          type="com.lwy.action.UpdatePwdAction">
          <set-property property="cancellable" value="true" />
          <forward name="updatePwd" path="/updatePwd.jsp" />
          <forward name="stuUpdatePwd" path="/stu/stuUpdatePwd.jsp" />
        </action>  </action-mappings>
      
      <message-resources parameter="ApplicationResources" />
      <!-- 加载Spring插件 -->
    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
    </plug-in>
    </struts-config>
    现在的情况是,别的action,如RegAction可以调用UserLoginDao,但是UserLoginAction调用UserLoginDao时UserLoginDao=null;
      

  27.   

    class就是指向实现类的  怎么可能是指向借口呢??