Java 在我的第一个spring项目中出现404错误?
我刚刚进入SpringMVC,我想写我的第一个Spring控制器来测试我在上面学到的东西。我做了一些基本的东西,只发送回一个消息,但当我点击网页中的按钮触发get请求并隐式地调用控制器时,我得到了一个404错误。我已经检查了spring引用和它们的示例,但是找不到任何解释为什么会出现这种错误。所以我来这里寻求帮助。这是我的密码: web.xml:Java 在我的第一个spring项目中出现404错误?,java,spring,jsp,servlets,Java,Spring,Jsp,Servlets,我刚刚进入SpringMVC,我想写我的第一个Spring控制器来测试我在上面学到的东西。我做了一些基本的东西,只发送回一个消息,但当我点击网页中的按钮触发get请求并隐式地调用控制器时,我得到了一个404错误。我已经检查了spring引用和它们的示例,但是找不到任何解释为什么会出现这种错误。所以我来这里寻求帮助。这是我的密码: web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln
<?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/applicationContext.xml</param-value>
</context-param>
<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>
</web-app>
index.jsp:
<%--
Created by IntelliJ IDEA.
User: Adrian
Date: 6/6/2018
Time: 12:05 PM
To change this template use File | Settings | File Templates.
--%>
<html>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Get your greeting <a href="/greeting.form">here</a></p>
</body>
</html>
入门:提供Web内容
收到你的问候
更新:以下是项目结构的屏幕截图:您正在使用过时的教程。从《Spring Boot入门指南》开始,我个人建议使用Thymeleaf而不是JSP,因为JSP在现代应用程序中有严重的局限性。@chrylis您能详细介绍一下现代应用程序中JSP的局限性吗?谢谢,请添加您的项目结构screenshot@lucumt我已经添加了截图
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:component-scan base-package="Controllers"/>
</beans>
package Controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class MainController {
@GetMapping("/greeting.form")
public String greeting(@RequestParam(name="name", required=false,
defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "greeting";
}
}
<%--
Created by IntelliJ IDEA.
User: Adrian
Date: 6/6/2018
Time: 12:05 PM
To change this template use File | Settings | File Templates.
--%>
<html>
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Get your greeting <a href="/greeting.form">here</a></p>
</body>
</html>