【问题标题】:No mapping found for HTTP request with URI xxx in DispatcherServlet with name xx在名称为 xx 的 DispatcherServlet 中找不到具有 URI xxx 的 HTTP 请求的映射
【发布时间】:2016-06-01 00:02:32
【问题描述】:

我查找了几乎所有与该错误相关的主题,但没有一个对我有用。

我创建了一个简单的 Spring MVC 项目,当我运行该项目时出现此错误:

févr. 19, 2016 12:29:56 PM org.springframework.web.servlet.PageNotFound noHandlerFound
AVERTISSEMENT: No mapping found for HTTP request with URI [/SpringMVCsample1/] in DispatcherServlet with name 'one'

这是我的代码:

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringMVCsample1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>jsp/hello.jsp</welcome-file>
  </welcome-file-list>



   <display-name>Spring MVC Application</display-name>

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

   <servlet-mapping>
      <servlet-name>one</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: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">

    <!-- This will allow Spring to load all the components from package com.capgemini.springtest   -->
    <!-- and all its child packages. -->
   <context:component-scan base-package="com.capgemini.springtest" />

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

</beans>

OneController.java:

package com.capgemini.springtest;


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
public class OneController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String printHello(ModelMap model) {

        model.addAttribute("message", "Hello Spring MVC Framework!");

        return "hello";
    }

}

你好.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
   <h2>${message}</h2>
</body>
</html>

有什么想法吗?

【问题讨论】:

  • 那么你的电话应该是 SpringMVCsample1/hello 而不是 SpringMVCsample1/one
  • 你指的是什么电话?

标签: java spring jsp spring-mvc model-view-controller


【解决方案1】:

在 web xml 中更改 url 模式:

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

将控制器更改为具有根上下文的映射:

package com.capgemini.springtest;


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
public class OneController {

    @RequestMapping(value = {"/","/hello"}, method = RequestMethod.GET)
    public String printHello(ModelMap model) {

        model.addAttribute("message", "Hello Spring MVC Framework!");

        return "hello";
    }

}

hello.jsp 应该位于文件夹下:/WEB-INF/jsp/hello.jsp

【讨论】:

  • 在文件夹下有 hello.jsp : /WEB-INF/jsp/hello.jsp 有帮助,但为什么它应该在那个文件夹下?而且,${message} 没有显示任何内容,为什么?
  • 因为您提到您的 jsp 将驻留在 one-servlet.xml 中的此文件夹 /WEB-INF/jsp/ 下:
【解决方案2】:

当然你会遇到这样的问题,因为你没有任何控制器来处理对“/”的请求,你要做的就是在你的 web 文件夹下添加一个控制器或添加 index.jsp

p>
web
|--->WEB-INF
|    |--->jsp
|        |---->index.jsp
|--->index.jsp

【讨论】:

  • 这是我的文件夹组织:|--->WEB-INF | |--->jsp | |---->hello.jsp
  • 在WEB-INF旁边创建index.jsp
  • 像魔术一样工作!如果我想添加一个控制器,我该如何进行?
  • 仍然,${message} 没有显示任何消息!为什么?
  • 好的,你刚刚创建的jsp页面正在处理对“localhost:8080/SpringMVCsample1”的请求,向你输入“localhost:8080/SpringMVCsample1/hello”的控制器发出请求
【解决方案3】:

web.xml 中,您的servlet 仅映射/ URI。 URL 模式应该是/*

【讨论】:

  • 令我困惑的是您的错误消息中提到的/SpringMVCsample1/ URI。就像它被认为是(或应该)由/hello 而不是Web 应用程序名称终止的URI 的一部分。您的应用程序以哪个名称部署在您的应用服务器中,您用来调用 /hello 页面的完整 URL 是什么?
  • 我的应用部署在:localhost:8080/SpringMVCsample1 并且 /hello 路径未知,404 ...
【解决方案4】:

尝试将&lt;mvc:annotation-driven /&gt; 添加到one-servlet.xml。请记住在此处添加mvc 命名空间的定义以及xmlns:mvc="http://www.springframework.org/schema/mvc"

然后尝试访问localhost:8080/SpringMVCsample1/hello。不是localhost:8080/SpringMVCsample1/hello/ 就像你说你在其中一个 cmets 中尝试过的那样

【讨论】:

  • 已经存在。不需要
  • @kunal-surana 我认为context:component-scan 用于声明包含 Spring 能够自动装配的组件的包和子包。 mvc:annotation-driven用于识别@Controller@RequestMapping@ResponseBody等注解。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-04
  • 2013-11-19
  • 2014-12-23
  • 2016-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多