javamavenspring-mvcubuntu-16.04eclipse-neon

404 Error - Java Spring MVC using Maven in Eclipse from Tutorial



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


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.


My File-Structure: file_structure

My 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 -->

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>

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

<?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>

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

<%@ 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>

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

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


Solution

  • Thanks for your answer,

    I solved the problem with following steps :

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

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

    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 ...

    That solution worked!!