0

我正在学习 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;
}

根上下文.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 模板。我不确定bean没有被注入控制器,所以当它提交的表单正确重定向时,如果我在控制器中硬编码感谢它可以工作。

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

*编辑 好吧,也许它与驱动的注释有关,但我仍然不确定,我无法让这个东西工作。所以控制器不能从 root-context.xml 中获取一个 bean?或者任何人都可以告诉我如何使用自动连线来做到这一点,这个想法只是不要在控制器上硬编码thanksViewName 的值。

4

1 回答 1

0

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

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

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

于 2013-07-12T02:16:12.853 回答