【问题标题】:Sprinv MVC No mapping found for HTTP request with URISpring MVC 找不到带有 URI 的 HTTP 请求的映射
【发布时间】:2017-04-15 21:48:08
【问题描述】:

我刚开始学习 Spring MVC,一直在关注这个 youtube 系列 https://www.youtube.com/watch?v=BkRZfxznaOo&index=1&list=PLsyeobzWxl7rjSO6xX00UWmVhL90i-cOk

我只是想创建一个简单的项目,以查看是否可以正确映射到控制器。现在,我们的目标是调用控制器并将一个字打印到控制台,此时调用以下 URL:localhost:8080/Leonardo/login

但是,我不断收到以下错误:警告:在 DispatcherServlet 中找不到带有 URI [/Leonardo/login] 的 HTTP 请求的映射,名称为 'leonardo'

我一直在 StackOverflow 上搜索其他问题/答案,常见的解决方法是在我的 web.xml 文件的“url-pattern”中添加“/”,但这不起作用。任何帮助是极大的赞赏。我刚刚完成了 youtube 系列中的最低限度的操作,并且在该视频中运行良好,但不适用于我。

项目结构:

Web.xml

 <web-app>
      <display-name>Archetype Created Web Application</display-name>

      <servlet>
           <servlet-name>leonardo</servlet-name>
           <servletclass>
                 org.springframework.web.servlet.DispatcherServlet
           </servlet-class>
     </servlet>
     <servlet-mapping>
          <servlet-name>leonardo</servlet-name>
          <url-pattern>/</url-pattern>
     </servlet-mapping>

</web-app>

leonardo-servlet.xml

 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:ctx="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-2.5.xsd 
   ">

        <ctx:annotation-config></ctx:annotation-config>
        <ctx:component-scan base-
         package="org.htech.leonardo.controllers"></ctx:component-scan>
   </beans>

HomeController.java

 package org.htech.leonardo.controllers;

 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;

 @Controller
 public class HomeController {

     @RequestMapping("/login")
     public void login() {
          System.out.print("HAHAHAH");
     }
 }

【问题讨论】:

  • 如果您将 Java 源文件放在 src/main/resources 而不是 src/main/java 下,它们甚至不会被编译。
  • 成功了,非常感谢,非常感谢
  • 另外请注意,该教程显然已经过时了——现在,只需使用 Spring Boot,不必担心任何 XML 文件、容器或复杂的配置。

标签: java spring-mvc


【解决方案1】:

首先确保您的根 URL 是 localhost:8080/Leonardo/。 你怎么知道?运行您的项目并检查浏览器上显示的 URL。

之后尝试点击 URL {your_root_url}/login 而不是 localhost:8080/Leonardo/login

根 URL 通常只是 localhost:8080/
所以你可以尝试点击localhost:8080/login 而不是localhost:8080/Leonardo/login

【讨论】:

    【解决方案2】:

    控制器中的login 方法映射到您的{base url}/login。当您运行应用程序时,浏览器中显示的第一个 url 就是您的基本 url。

    【讨论】:

      猜你喜欢
      • 2013-09-16
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      • 2013-07-27
      • 2012-10-26
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多