【发布时间】:2018-05-08 08:43:06
【问题描述】:
我的系统: 操作系统:Ubuntu Linux 16.04 IDE:带有 Servlet 引擎的 Eclipse Neon:Apache Tomcat/8.0.48
我尝试通过使用 Maven 和示例的教程来学习 Spring MVC。 我准确地遵循了这些步骤,但我的浏览器出现 404 - 错误。
我的文件结构: file_structure
我的 pom.xml
<!-- lines ommited -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>DemoMVC2</finalName>
</build>
<!-- lines ommited -->
这是我的 web.xml
<!-- lines ommited -->
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>telusko</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>telusko</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
我的 AddController.java
package com.telusko;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class AddController {
@RequestMapping("/add")
public String add(){
return "display.jsp";
}
}
telusko-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="com.telusko"></ctx:component-scan>
</beans>
我的 index.jsp
<html>
<body>
<h2>MVC Tutorial second attempt!</h2>
<form action="add"><!-- call servlet "add" Class AddController method "add"-->
<input type="text" name="t1"><br>
<input type="text" name="t2"><br>
<input type=submit>
</form>
</body>
</html>
display.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>I'm here</p>
</body>
</html>
当我在服务器上运行 index.jsp 时,它会显示在浏览器上。 但是在填写表格并通过提交按钮发送数据后 它显示了这个 404 错误:
我多次尝试这个教程。我还尝试了评论部分中的错误修复以及 stackoverflow 上的类似帖子,但没有任何效果。
【问题讨论】:
-
你有没有注意到这个官方教程docs.spring.io/spring/docs/current/spring-framework-reference/… servlet-mapping 和你的不一样?您正在指示您的应用程序将以 *.html 结尾的请求映射到您的 DispatcherServlet 并且
/add不匹配。 -
感谢您提供的信息!
标签: java maven spring-mvc ubuntu-16.04 eclipse-neon