【问题标题】:404 Error - Java Spring MVC using Maven in Eclipse from Tutorial404 错误 - Java Spring MVC 在 Eclipse 中使用 Maven 教程
【发布时间】: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 错误:

404_error_page

我多次尝试这个教程。我还尝试了评论部分中的错误修复以及 stackoverflow 上的类似帖子,但没有任何效果。

【问题讨论】:

标签: java maven spring-mvc ubuntu-16.04 eclipse-neon


【解决方案1】:

你使用了JSP 而不是HTML,所以你应该像这样设置url-pattern:

<servlet-mapping>
    <servlet-name>telusko</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

【讨论】:

  • 感谢您的回答。这与将 tomcat-configuration 中的 app-version 设置为 2.5(我的答案中的详细信息)相结合,欢迎
【解决方案2】:

谢谢你的回答,

我通过以下步骤解决了问题:

首先我用 / 编辑了 WEB-INF/web.xml :

<url-pattern>/</url-pattern>

并将tomcat-configuration中的web-app版本从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">

最后右键项目并运行->在服务器上运行...

那个解决方案奏效了!!

【讨论】:

    【解决方案3】:

    当 Java 构建路径不接触 Eclipse 中的 maven 依赖项时,问题就出现了。请参见下面的屏幕:

    所以选择那些 maven 依赖项并重新启动服务器。它会起作用的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-07
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 2015-07-09
      • 2012-08-03
      相关资源
      最近更新 更多