Home > OS >  Spring MVC - Tomcat return constant 404 errors
Spring MVC - Tomcat return constant 404 errors

Time:08-07

I just started Spring MVC with Maven and when I send a request Tomcat always returns a 404 not found result. This is my simple controller with configuration files.

HelloController.java:

package com.mvcdemo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloController {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String printHelloWorld(ModelMap modelMap) {
        modelMap.addAttribute(
                        "message",
                        "Hello World and Welcome to Spring MVC!");
        return "hello";
    }

}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
                 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-   app_3_0.xsd"
                 id="WebApp_ID" version="3.0">

    <display-name>Spring MVC Application</display-name>

    <!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

spring-dispatcher-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
      http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

  <!-- FIXME    Change name of the package to your base-package -->
  <context:component-scan base-package="com.mvcdemo"/>
  <mvc:annotation-driven/>

  <bean >
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp" />
  </bean>

</beans>

hello.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>Spring MVC Demo</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>

Is the problem located in bad configuration of the web.xml or spring-dispatcher-servlet.xml files that prevent the URL to be mapped?

CodePudding user response:

You need to check what kind of version you have for Tomcat. If it's 10, you need to downgrade it to 9. Common case when having this error.

CodePudding user response:

You can just get project setup quickly from https://start.spring.io/

  • Related