【问题标题】:Spring MVC - No mapping found for HTTP requestSpring MVC - 找不到 HTTP 请求的映射
【发布时间】:2013-12-20 00:33:32
【问题描述】:

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

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

所以我编写了 servlet (mvc-dispatcher-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) 中,但在搜索谷歌后我没有可行的解决方案。希望对您有所帮助!

【问题讨论】:

    标签: java xml spring servlets spring-mvc


    【解决方案1】:

    你还没有声明

    <mvc:annotation-driven />
    

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

    【讨论】:

    • 我试过这个,但我得到“元素“mvc:annotation-driven”的前缀“mvc”未绑定。”错误。我还应该向 servlet 添加什么?
    • @user3062819 这就是我所说的和相应的命名空间。您需要像 contextbeans 一样添加 mvc 命名空间。
    • 好的。我添加了 xmlns:mvc="springframework.org/schema/mvc" xsi:schemaLocation="springframework.org/schema/mvc springframework.org/schema/mvc/spring-mvc-3.0.xsd",现在我可以使用 mvc,但同样的警告与“找不到映射”
    • @user3062819 这最终可能会对您的应用程序产生其他影响。现在,您的ContextLoaderListenerDispatcherServlet 都在从/WEB-INF/mvc-dispatcher-servlet.xml 加载上下文。您将需要删除 &lt;context-param&gt;ContextLoaderListener。您目前不需要它们。
    • 正如你所建议的,我从 web.xml 中删除了 ,但仍然是同样的问题
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 2016-02-17
      • 1970-01-01
      相关资源
      最近更新 更多