我刚进入Spring MVC,我想编写我的第一个弹簧控制器来测试我学到的东西。我做了一些基本的东西,只是发送回按摩,但当我点击网页中的按钮来触发获取请求和隐含控制器我得到404错误。我检查了弹簧参考和他们的例子,但找不到任何解释为什么我得到了这个错误。所以我来这里寻求帮助。这是我的代码: web.xml:
<?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>
dispatcher-servlet.xml:
<?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>
appliationContext.xml:
<?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>
MainController:
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";
}
}
的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>
更新:这是包含项目结构的屏幕截图:https://imgur.com/a/tj7o0DA