bean没有注入Spring MVC

时间:2013-07-11 21:51:28

标签: spring spring-mvc

我正在学习Spring MVC,我正在研究一个基本的表单示例,但我不知道为什么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>

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>

Servlet的context.xml中

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

我使用STS作为IDE并包含Spring mvc模板。 我不确定它没有被注入到控制器中,所以当它提交的表单正确地重定向时,如果我在控制器中硬编码感谢就可以了。

在此先感谢您的帮助,我知道这可能是一个基本错误,所以,谢谢。

*的 修改 好吧,也许它与注释驱动有关,但我仍然不确定,我还没有能够使这个东西工作。 所以控制器无法从root-context.xml中提取bean? 或者任何人都可以告诉我如何使用自动线,这个想法只是为了不在控制器上硬编码thanksViewName的值。

1 个答案:

答案 0 :(得分:0)

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

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

我的建议是下载Spring Stool Suite(STS)并创建一个新的spring模板项目(选择 MVC模板),它将创建一个可运行的项目,通过这种方式,您可以看到所有内容是如何组合在一起的。