Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么在服务器上正确注入的Springbean在我将表单发布到控制器时启动时为空?_Java_Hibernate_Spring - Fatal编程技术网

Java 为什么在服务器上正确注入的Springbean在我将表单发布到控制器时启动时为空?

Java 为什么在服务器上正确注入的Springbean在我将表单发布到控制器时启动时为空?,java,hibernate,spring,Java,Hibernate,Spring,为什么在服务器启动时正确注入的springbean(loginInfoDAO)在我将表单发布到控制器时为空?我已经在启动时通过了二传手,他们正在正确地注射。但是,我运行get方法,然后运行post方法,注入的值为null。为什么会发生这种情况 控制器 @Controller @RequestMapping("/login") public class LoginController extends BaseController{ private LoginInf

为什么在服务器启动时正确注入的springbean(loginInfoDAO)在我将表单发布到控制器时为空?我已经在启动时通过了二传手,他们正在正确地注射。但是,我运行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>

com.mysql.jdbc.Driver
jdbc:mysql://localhost/databasename
数据库用户名
数据库密码
com.projectname.model.LoginInfo
com.projectname.model.User
org.hibernate.dialogue.mysqldialogue
真的
真的
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>

/WEB-INF/messages/messages

添加@Autowired for loginInfoDAO,因为您使用的是
上下文:注释配置

@Autowired
private LoginInfoDAO loginInfoDAO;
然后从上下文xml中删除以下条目(因为它们是由这里的注释驱动的)



这会让你走起来。

没问题-很高兴它帮了你!此外,如果要自动连接application-context.xml,则不需要在application-context.xml中使用名为
myLoginInfoDAO
/login
的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>