2012-05-30 49 views
0

利用SpringSource工具套件2.9.2,我創建了Spring MVC的組件掃描

新建>彈簧模板工程> Spring MVC的項目

這生成一個簡單的控制器項目,pom.xml中,servlet-context.xml,root-context.xml以及控制器映射到的簡單jsp文件。

然而,當我運行項目時,雖然/resources/**路徑被映射到servlet-context.xml中指定的資源servlet,但組件掃描未拾取控制器。應該給我你好世界的頁面是給我一個404.

我做錯了什麼?

servlet-context.xml的相關位:

<context:component-scan base-package="com.test.hello" /> 

com.test.hello.HomeController.java

@Controller 
public class HomeController { 
    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String home(Locale locale, Model model) { 
     model.addAttribute("serverTime", new Date()); 
     return "home"; 
    } 
} 

和錯誤消息:

警告:org.springframework.web.servlet.PageNotFound - 無使用名稱'appServlet'在DispatcherServlet中爲HTTP請求使用URI [/]找到的映射

內容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> 
+0

你能在這裏告訴我你的web.xml文件。 –

+0

您是否將名稱爲appServlet的DispatcherServlet配置爲? –

+0

你的servlet-context.xml中是否有''? –

回答

2

一個簡單的清理並生成項目已經修復了這個問題對我來說!

0

請修改這一行<context:component-scan base-package="com.test.hello.*" />

它不掃描您的控制器還請指定應用程序使用的是什麼網址