404错误-教程中的Eclipse中使用Maven的Java Spring MVC [英] 404 Error - Java Spring MVC using Maven in Eclipse from Tutorial

查看:127
本文介绍了404错误-教程中的Eclipse中使用Maven的Java Spring MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的系统: 操作系统:Ubuntu Linux 16.04 IDE:带有Servlet引擎的Eclipse Neon:Apache Tomcat/8.0.48

My System: OS: Ubuntu Linux 16.04 IDE: Eclipse Neon with Servlet Engine: Apache Tomcat/8.0.48

我尝试通过使用Maven和Example的教程来学习Spring MVC. 我正确地遵循了这些步骤,但是浏览器出现404-错误.

I try to learn the Spring MVC via the Tutorial using Maven with Example. I followed these steps accurately but i get an 404 - Error on my Browser.

我的文件结构: 文件结构

我的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

heres my 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

my 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

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

my 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

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错误:

When i run the index.jsp on server, it is displayed on the browser. But after filling the form, and sending the data via submit button it show this 404 error:

404_error_page

我多次尝试了本教程.我也尝试了注释部分中的错误修正以及关于stackoverflow的类似文章,但没有任何效果.

I tried this Tutorial many times. I tried also the bugfixes in the comment section and similar posts on stackoverflow, but nothing worked.

推荐答案

感谢您的回答

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

I solved the problem with following steps :

首先,我用/修改了WEB-INF/web.xml中的内容:

First i edited the in WEB-INF/web.xml with an / :

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

然后将tomcat配置中的Web应用程序版本从3.1设置为2.5 在本地tomcat配置中-> web.xml

And set the web-app version from the tomcat-configuration from 3.1 to 2.5 in the local tomcat configuration -> 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">

最后右键单击该项目,然后以运行方式->在服务器上运行...

Finally right-clicked on the Project and run-as -> run on server ...

该解决方案有效!

这篇关于404错误-教程中的Eclipse中使用Maven的Java Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆