【问题标题】:No mapping found for HTTP request with uri in DispatcherServlet with name 'spring' [duplicate]在名为“spring”的 DispatcherServlet 中找不到带有 uri 的 HTTP 请求的映射 [重复]
【发布时间】:2015-03-16 18:35:02
【问题描述】:

我是 Spring 框架的新手。我正在尝试一个非常基本的例子。 这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>    
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://java.sun.com/xml/ns/javaee"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd    " id="WebApp_ID" version="3.0">    
  <display-name>MovieDB</display-name>    
  <servlet>    
    <servlet-name>movie-dispatcher</servlet-name>    
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
        <load-on-startup>1</load-on-startup>    
  </servlet>    
  <servlet-mapping>    
    <servlet-name>movie-dispatcher</servlet-name>    
        <url-pattern>/</url-pattern>    
  </servlet-mapping>    
</web-app>    

这是我的电影调度程序-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       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-4.0.xsd             
    http://www.springframework.org/schema/context            
    http://www.springframework.org/schema/context/spring-context-4.0.xsd    ">
    <context:component-scan base-package="com.moviedb.controller" />
    <bean id="viewResolver"      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>  

这是我的控制器

package com.moviedb.controller;    
import org.springframework.stereotype.Controller;    
import org.springframework.web.bind.annotation.RequestMapping;    
import org.springframework.web.servlet.ModelAndView;    
@Controller    
public class MovieDBController {        
    @RequestMapping("/homepage")    
    public ModelAndView getHomePage(){    
        ModelAndView model = new ModelAndView("homepage");    
        return model;           
    }       
    @RequestMapping("/movie")    
    public ModelAndView getMovieDetailsPage(){    
        //code              
    }    
}    

当我在浏览器中输入此网址时http://localhost:8080/MovieDB/homepage

我收到 404 错误和

org.springframework.web.servlet.PageNotFound noHandlerFound 警告: 未找到带有 URI [/MovieDB/homepage] 的 HTTP 请求的映射 在名为“movie-dispatcher”的 DispatcherServlet 中

在我的服务器控制台中。

我无法弄清楚这里出了什么问题

【问题讨论】:

  • 你试过localhost:8080/homepage吗?
  • 您需要在 web.xml 中为电影调度程序 servlet 提供 URL 模式为 /MovieDB/*,以便您提供的 url 正常工作。如果您不更改 web.xml,@AleksanderBlomskøld 网址应该可以工作
  • 是但没有运气。还是同样的错误。

标签: java xml spring spring-mvc servlets


【解决方案1】:

我认为您在movie-dispatcher-servlet.xml 文件中缺少以下行。

<mvc:annotation-driven/>

还有一点将web.xml中的url pattern(/)改成下面。

<url-pattern>/*</url-pattern>

【讨论】:

  • 你能发布你更新的movie-dispatcher-servlet.xml(如果你已经完成的话)文件吗?
  • contextConfigLocation/WEB-INF/movie-dispatcher-servlet.xml上下文参数> org.springframework.web.context.ContextLoaderListener
  • 我刚刚将监听器添加到已经存在的 web.xml 中
猜你喜欢
  • 2015-06-03
  • 2013-09-12
  • 2016-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-19
  • 2017-09-17
相关资源
最近更新 更多