Spring MVC-找不到HTTP请求的映射



在我的工作中,Spring MVC遇到了一个很大的问题。我已经从我们的SVN下载了示例项目。它包含了带有Maven的简单Spring项目结构。示例项目运行良好。Tomcat启动了,我在http://localhost:8080/XXX/上看到了漂亮的"Hello World"页面(来自index.jsp)。

问题是,我想写一个控制器来处理,例如"/welcome"。

所以我编写了servlet(mvc调度器servlet.xml),更新了我的web.xml文件,添加了控制器。但每次我写"http://localhost:8080/XXX/welcome"时,我都会得到

WARNING: No mapping found for HTTP request with URI [/XXX/welcome] in DispatcherServlet with name 'mvc-dispatcher'

这是我的配置

mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="main.java" />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Spring Web MVC Application</display-name>
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

HelloWorldController.java

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/welcome")
public class HelloWorldController {
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Spring 3 MVC Hello World");
        return "hello";
    }

这是我的项目结构(对我来说,这与我习惯的不同,但这并不取决于我)。http://postimg.org/image/p649rxbjr/

我想,问题出在web.xml(servlet conf)中,但在搜索谷歌后,我没有可用的解决方案。希望你帮忙!

您尚未声明

<mvc:annotation-driven />

在您的上下文中(以及相应的命名空间)。因此,Spring不会向DispatcherServlet注册任何处理程序。您需要添加它。

我遇到了同样的问题,我尝试了所有方法。最终起作用的不是XXX部分,而是http://localhost:8080/welcome