404 错误 - Java Spring MVC 在教程中的 Eclipse 中使用 Maven
404 Error - Java Spring MVC using Maven in Eclipse from Tutorial
我的系统:
OS: 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 错误:
404_error_page
我多次尝试这个教程。我也尝试了评论部分中的错误修复和 Whosebug 上的类似帖子,但没有任何效果。
你使用了 JSP
而不是 HTML
所以你应该像这样设置 url-pattern :
<servlet-mapping>
<servlet-name>telusko</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
感谢您的回答,
我通过以下步骤解决了问题:
首先,我在 WEB-INF/web.xml 中编辑了一个 / :
<url-pattern>/</url-pattern>
并将 tomcat 配置中的网络应用程序版本从 3.1 设置为 2.5
在本地 tomcat 配置中 -> web.xml
<web-app version="2.5" 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_2_5.xsd">
最后右键单击项目并 运行-as -> 运行 on server ...
该解决方案有效!!
当 Java 构建路径没有与 eclipse 中的 maven 依赖项联系时,问题就来了。见下图:
所以 select 那些 maven 依赖项并重新启动服务器。它会起作用。
我的系统: OS: 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 错误:
404_error_page
我多次尝试这个教程。我也尝试了评论部分中的错误修复和 Whosebug 上的类似帖子,但没有任何效果。
你使用了 JSP
而不是 HTML
所以你应该像这样设置 url-pattern :
<servlet-mapping>
<servlet-name>telusko</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
感谢您的回答,
我通过以下步骤解决了问题:
首先,我在 WEB-INF/web.xml 中编辑了一个 / :
<url-pattern>/</url-pattern>
并将 tomcat 配置中的网络应用程序版本从 3.1 设置为 2.5 在本地 tomcat 配置中 -> web.xml
<web-app version="2.5" 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_2_5.xsd">
最后右键单击项目并 运行-as -> 运行 on server ...
该解决方案有效!!
当 Java 构建路径没有与 eclipse 中的 maven 依赖项联系时,问题就来了。见下图:
所以 select 那些 maven 依赖项并重新启动服务器。它会起作用。