Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
SpringMVC上没有注入bean_Spring_Spring Mvc - Fatal编程技术网

SpringMVC上没有注入bean

SpringMVC上没有注入bean,spring,spring-mvc,Spring,Spring Mvc,我正在学习SpringMVC,我正在做一个基本的表单示例,但是我不知道为什么bean没有正确地注入信息,所以我想知道是否有人可以指导我 控制器 package com.carloscortina.Test; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework

我正在学习SpringMVC,我正在做一个基本的表单示例,但是我不知道为什么bean没有正确地注入信息,所以我想知道是否有人可以指导我

控制器

package com.carloscortina.Test;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.carloscortina.toy.model.Member;

@Controller
public final class NomineeController {

private static final Logger log=
        Logger.getLogger(NomineeController.class);

private String thanksViewName ="thanks";

public void setThanksViewName(String thanksViewName) {
    this.thanksViewName = thanksViewName;
}

@RequestMapping(method = RequestMethod.GET)
public Member form() { return new Member();}

@RequestMapping(method = RequestMethod.POST)
public String processFormData(Member member){
    log.info("Processing nominee: " + member);
    log.info("thanksViewName: " + thanksViewName);
    return thanksViewName;
}
root-context.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.xsd">

<!-- Root Context: defines shared resources visible to all other web components -->

<bean id="formAnswer"
    class="com.carloscortina.Test.NomineeController"
    p:thanksViewName="thanks" /> 

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.carloscortina.Test" />

Web.xml

<?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">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

上下文配置位置
/WEB-INF/spring/root-context.xml
org.springframework.web.context.ContextLoaderListener
appServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/appServlet/servlet-context.xml
1.
appServlet
/
Servlet-context.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.xsd">

<!-- Root Context: defines shared resources visible to all other web components -->

<bean id="formAnswer"
    class="com.carloscortina.Test.NomineeController"
    p:thanksViewName="thanks" /> 

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.carloscortina.Test" />

im使用STS作为IDE,包括SpringMVC模板。 我不确定bean没有被注入到控制器中,所以当表单提交时,它会正确重定向,如果我在控制器中硬编码感谢,它就会工作

提前谢谢你的帮助,我知道这可能是一个基本的错误,所以谢谢你

*编辑 好吧,也许它与注释驱动有关,但我仍然不确定,我还没能使这件事起作用。 那么控制器不能从root-context.xml创建bean了吗?
或者,任何人都可以告诉我如何使用auto wire,其想法是不要在控制器上硬编码thanksViewName的值。

您需要将以下内容添加到servlet-context.xml配置文件中,您不需要在xml文件中声明带注释的bean

<mvc:annotation-driven />
<context:component-scan base-package="com.carloscortina" />


我的建议是下载SpringStool套件(STS)并创建一个新的Spring模板项目(选择MVC模板),它将创建一个可运行的项目,这样您就可以看到所有内容是如何组合在一起的。

为什么要用
@Controller
注释bean?似乎您是通过组件扫描定义控制器,所以它没有在XML中配置,这就是原因。我想说我理解,但我没有。我在@Controller中使用组件扫描,这意味着我不能在XML中声明bean并注入它?谢谢你的回答。你用什么方法来确定bean没有被注入?也许我误用了这个术语,但我认为bean没有被注入,因为在日志中变量thanksViewName为null,页面也不会显示,但当我硬编码thanksViewName时,日志中的值不为null。您是否将spring配置包含到web.xml中?有spring的日志吗?我使用STS和模板,我试图做的是将视图与逻辑分开,我的意思是,控制器不知道表单提交后将重定向到哪里,通过示例,我遵循将信息放在XML中的想法,但它对我不起作用。