2013-07-11 80 views
0

即時學習Spring MVC和我正在研究一個基本的表單示例,但我不知道爲什麼一個bean沒有正確注入信息,所以我想知道是否有人可以指導我。bean沒有注入彈簧MVC

控制器

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; 
} 

根的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模板。 即時通訊不知道該bean沒有被注入到控制器中,所以當它提交的表單正確重定向時,如果我在控制器中硬編碼感謝,它就會工作。

在此先感謝您的幫助,我知道這可能是一個基本的錯誤,所以謝謝。

* 編輯 哦,也許這涉及到驅動的註釋,但我仍然不知道,我還沒有能夠讓這件事的工作。 因此,控制器無法從root-context.xml中找到一個bean? 或任何人都可以告訴我如何使用自動接線,這個想法只是爲了不在控制器上硬編碼thanksViewName的值。

+0

你爲什麼用'@ Controller'註釋你的bean?看起來你正在通過'component-scan'定義你的控制器,所以它沒有在XML中配置,這就是爲什麼。 –

+0

我想說,我明白,但我沒有。即時通訊使用組件掃描@Controller,這意味着我不能在XML中聲明一個bean並注入它? 謝謝你的回答。 – Ccortina

+0

你用什麼方法來確定這個bean沒有被注入? – gerrytan

回答

0

您需要將以下內容添加到您的servlet-context.xml配置文件中,而不需要在XML文件中聲明帶註釋的bean。

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

我的建議是下載春凳套件(STS),並創建一個新的春天模板項目(選擇MVC模板),它會創建一個可運行的項目,這樣你可以看到一切都放在一起。

+0

即時消息STS和模板,以及即時嘗試將其從邏輯單獨視圖我的意思是控制器不知道表單提交後將重定向的位置,並通過示例我遵循這個想法它把在XML的信息,但它只是沒有與我一起工作。 – Ccortina