Akki
Akki

Reputation: 29

Http status code 404 spring mvc

I am new to Spring MVC and I am just trying a basic example for it. But I am receiving Http Stats 404 error. My files are as below :

It is a maven project.

/Web-Inf/spring-mvc-demo-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">

    <!-- Step 3: Add support for component scanning -->
    <context:component-scan base-package="springdemo" />

    <!-- Step 4: Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Step 5: Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

then I have WEB-INF/web.xml

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

    <display-name>spring-mvc-demo</display-name>

    <!-- Spring MVC Configs -->

    <!-- 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-mvc-demo-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>

/WEB-INF/view/main-page.jsp

<html>

<body>

<h1>Welcome to home page!!!!</h1>

</body>

</html>

and then src/HomeController.java

package springmvcdemo;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

@Component
public class HomeController {

    @RequestMapping("/")
    public String startPage(){
        return "main-page";
    }
}

I am creating a Dynamic Web Project in Eclipse IDE and it is a Maven project and server on which I am trying to run it is Tomcat 8.0.44

Can anybody help me in eliminating the error?

Upvotes: 0

Views: 441

Answers (3)

shanedust
shanedust

Reputation: 54

Your example looks like it doesn't have proper xsd version and may be due to jar file version

Better try this example

Spring 4 mvc hello world example

Upvotes: 0

Anil Kumar Athuluri
Anil Kumar Athuluri

Reputation: 653

I see package name in spring-mvc-demo-servlet.xml is not matching with package you have the controller in.

spring-mvc-demo-servlet.xml:-

 <context:component-scan base-package="springdemo" />

HomeController,

package springmvcdemo;

Upvotes: 1

Phanindra Gopishetty
Phanindra Gopishetty

Reputation: 169

Since your servlet name is dispatcher in web.xml. spring will try to load configuration file named dispatcher-servlet.xml

In dispatcher-servlet.xml you must provide component-scan

Upvotes: 0

Related Questions