为什么在服务器上正确注入的springbean在我将表单发布到控制器时启动,为null



为什么在服务器上正确注入的springbean(loginInfoDAO)在我将表单发布到控制器时为null?我在起跑时穿过了二传手,他们正在正确地注射。然而,我运行了get方法,然后post方法和注入的值为null。为什么会发生这种情况?

控制器

    @Controller
    @RequestMapping("/login")
    public class LoginController extends BaseController{
    private LoginInfoDAO loginInfoDAO;
    public void setLoginInfoDAO(LoginInfoDAO loginInfoDAO) {
    this.loginInfoDAO = loginInfoDAO;
    }
    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView getLogin(@ModelAttribute("user") final User ur) {
        ModelAndView mav = new ModelAndView("/login/login");
        return mav;
    }
        @RequestMapping(method=RequestMethod.POST)
    public ModelAndView login(@ModelAttribute("user") final User ur) {
        loginInfoDAO.login(ur);
        ModelAndView mav = new ModelAndView();
        return mav;
    }
    }

application-context.xml

        <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <bean id="myDataSource" 
    class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
      <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
      </property>
      <property name="url">
        <value>jdbc:mysql://localhost/databasename</value>
      </property>
      <property name="username">
        <value>databaseusername</value>
      </property>
      <property name="password">
        <value>databasepassword</value>
      </property>
      <!-- Disable the second-level cache  -->
        <!-- Echo all executed SQL to stdout -->
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" /> 
        <property name="annotatedClasses">
            <list>
            <value>com.projectname.model.LoginInfo</value>
            <value>com.projectname.model.User</value>
            </list>
        </property> 
        <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
        </property>
    </bean>

    <bean id="myLoginInfoDAO" class="com.projectname.dao.LoginInfoDAOImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean name="/login" class="com.projectname.controllers.LoginController" >
        <property name="loginInfoDAO" ref="myLoginInfoDAO" />
    </bean>
    </beans>

spring-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<context:component-scan
    base-package="com.projectname" />
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <value>/WEB-INF/messages/messages</value>
    </property>
    <property name="cacheSeconds" value="60" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>
</beans>

为loginInfoDAO添加@Autowired,因为您使用的是context:annotation-config

@Autowired
private LoginInfoDAO loginInfoDAO;

然后从上下文xml中删除以下条目(因为它们是由此处的注释驱动的)。

   <bean id="myLoginInfoDAO" class="com.projectname.dao.LoginInfoDAOImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean name="/login" class="com.projectname.controllers.LoginController" >
        <property name="loginInfoDAO" ref="myLoginInfoDAO" />
    </bean>

这应该会让你继续前进。

最新更新