【发布时间】: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