No mapping found for HTTP request with URI [/MyTesting/] in DispatcherServlet with name 'mvc-dispatcher'

StackOverflow https://stackoverflow.com/questions/20814896

  •  22-09-2022
  •  | 
  •  

Question

I am new to spring mvc and Maven. I am trying to get a simple hello world example working. I don't see where I am going wrong it should be straight forward. I am using Maven 3, Eclipse Kepler, Tomcat 6 on a MAC OS.

Can Someone please see the below code and see what is wrong? When I hit localhost:8080/MyTesting I expect to get my welcome file on browser but I get this on the browser:

HTTP Status 404 -

type Status report

message

description The requested resource is not available.

on Eclipse console I get:

WARNING: No mapping found for HTTP request with URI [/MyTesting/] in DispatcherServlet with name 'mvc-dispatcher'

POM.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    http://maven.apache.org/xsd/maven 4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
  <groupId>MyTesting</groupId>
  <artifactId>MyTesting</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>

    <properties>
        <spring.version>3.1.1.RELEASE</spring.version>
    </properties>


    <dependencies>

        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

    </dependencies>
</project>

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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>MyTesting</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

mvc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:tx="http://www.springframework.org/schema/tx"
 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-3.1.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

        <context:component-scan base-package="main.com.mytesting" />

     <bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver" > 
          <property name="prefix" value="/WEB-INF/views/" />
          <property name="suffix" value=".jsp" /> 
     </bean>


  </beans>
Was it helpful?

Solution 4

Thanks Guys,

Its all good now after wasting a few hours of my dev time. I actually had a typo in my folder structures. My folder structure was main.com.mymesting.controllers hence the component scan was failing. I changed it to main.com.mytesting.controllers. It works fine now.

OTHER TIPS

  1. Start from this example from Spring developers and tweak it for yourself. It uses the newer, simpler Java Config (annotation-based) configuration.
  2. If you still want to debug your current code, can you push it to github? Your requestMapping in the controller may not be right.

From your post I did not saw any controller configuration, and the error 404 means the required resource is not there.

There are two ways to develop Spring web MVC projects annotation based or programatically. This is a tutorial on the programatic approach, and this is a tutorial on the annotation based approach.

I think you don't create controller for dispatcher map. I found this tutorial very helpful about create spring mvc.

Check your spring configuration file and folder structure "src/main/java"

<context:component-scan base-package="com.wenzinsapp.testapp.controller" /> is resembles as your folder structure

it could be the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top