spring - DispatcherServlet : No mapping found for HTTP request with URI

标签 spring spring-mvc tomcat servlet-mapping

在 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>

最佳答案

您的配置看起来不错。但是,您正在请求未映射到任何 Controller 处理程序方法的/CloudService/url。您的 Controller 有一个/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,您就需要配置它

关于spring - DispatcherServlet : No mapping found for HTTP request with URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37327073/

相关文章:

java.lang.IllegalStateException : File has already been moved - cannot be transferred again 错误

java - @Valid 不适用于嵌套对象(Java/Spring Boot)

java - {Spring boot + Hibernate + MySQL}无法获取数据库连接

java - 在 spring web 应用程序中创建数据源的正确方法是什么?

java - 如何在 Spring Boot 中指定回退请求映射

java - Grizzly (GlassFish) 和 Tomcat 中的最大 URI 长度

tomcat - 在 tomcat 上部署基于 reSTLet 的应用程序的问题

java - DBUnit 错误 - 缺少以下 bean : [dbUnitDatabaseConnection, 数据源之一]

spring - Apache Tiles 退休了。备择方案?

java - 在tomcat中创建一个文件