利用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>
你能在這裏告訴我你的web.xml文件。 –
您是否將名稱爲appServlet的DispatcherServlet配置爲? –
你的servlet-context.xml中是否有' '? –