【问题标题】:DispatcherServlet: No mapping found for HTTP request with URIDispatcherServlet:找不到带有 URI 的 HTTP 请求的映射
【发布时间】:2016-05-19 14:59:16
【问题描述】:

在 STS 中运行我的项目会在 Tomcat 上给出错误:

Mai 19, 2016 4:44:52 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNUNG: No mapping found for HTTP request with URI [/CloudService/] in DispatcherServlet with name 'HelloWeb'
Mai 19, 2016 4:45:35 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNUNG: No mapping found for HTTP request with URI [/CloudService/HelloWeb/hello] in DispatcherServlet with name 'HelloWeb'
Mai 19, 2016 4:48:13 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNUNG: No mapping found for HTTP request with URI [/CloudService/] in DispatcherServlet with name 'HelloWeb'

我已经查看了几乎所有关于 stackoverflow 的相关文章,但我无法解决我的问题。

src/main/java/HelloController.java

package com.stackoverflow;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController{

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");

      return "hello";
   }

}

WebContent/WEB-INF/HelloWeb-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="com.stackoverflow" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

WebContent/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>CloudService</display-name>

   <servlet>
      <servlet-name>HelloWeb</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>


</web-app>

WebContent/WEB-INF/jsp/hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>${message}</h2>
</body>
</html>

【问题讨论】:

  • 其中哪些没有帮助? stackoverflow.com/… 很抱歉重复了这么多,但是 [spring] 似乎没有任何管理员想要管理一个有价值的知识库。
  • 看来您未能引导 Spring 上下文...在 XML 配置中,web.xml 通常包含用于该用法的 &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; 块,默认情况下将使用(可能声明没有 bean ) applicationContext.xml 文件

标签: spring spring-mvc tomcat servlet-mapping


【解决方案1】:

您的配置看起来不错。但是,您正在请求未映射到任何控制器处理程序方法的 /CloudService/ url。你的控制器有一个 /CloudService/hello 的处理程序。您可以为 / 包含一个处理程序,或者如果 / 和 /hello 都应该由相同的方法处理,您可以这样做

@Controller
@RequestMapping({"/hello" , "/"})
public class HelloController{

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");

      return "hello";
   }

}

N.B 您的应用程序有一个应用程序上下文 HelloWeb-servlet.xml,因此您不需要 ContextLoaderListener 配置来创建父上下文。但是,一旦您有必须分离到不同上下文中的服务 bean,您需要配置它

【讨论】:

  • 我已按照您的建议更改了代码,我得到:HTTP 状态 500 - 处理程序处理失败;嵌套异常是 java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config - 当我调用时:localhost:8080/CloudService/hello 我还能做错什么?
  • 您需要确保在类路径中包含 jstl 库。看到这个帖子stackoverflow.com/questions/28026991/…
【解决方案2】:

这是一个示例 web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

这是 dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
   http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
   ">

    <context:component-scan base-package="controllers"/>
    <mvc:annotation-driven/>

    <bean id="viewResolver" class ="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <bean name="shape" class="shapes.Shape">
        <constructor-arg index="0" value ="12" />
        <constructor-arg index="1" value ="2" />
    </bean>
    <import resource="spring-dao.xml"/>
 </beans>

我相信这也适合你。

【讨论】:

    猜你喜欢
    • 2021-08-01
    • 2017-09-17
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2015-06-03
    • 2023-03-29
    相关资源
    最近更新 更多