No mapping found for HTTP request with URI.... in DispatcherServlet with name

SpringSpring Mvc

Spring Problem Overview


I checked out nearly every relevant article on stackoverflow already, but I just cant fix my problem.

Here is the code: web.xml:

   <display-name>Spring3MVC</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>contextConfigLocation</param-name> 
        <param-value>/WEB-INF/spring-servlet.xml</param-value> 
    </context-param> 
    <listener> 
        <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> 
    </listener>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
        <url-pattern>/</url-pattern>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
</web-app>

spring-servlet.xml:

<context:component-scan base-package="com.mycompany.elso" />
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>   
    
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

myController:

public class myController {
    @RequestMapping("/hello")
    public ModelAndView helloWorld() {
 
        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello", "message", message); 
    }
}

Web Pages/index.jsp:

<html>
<head>
    <title>Spring 3.0 MVC Series</title>
</head>
<body>
    <a href="hello.html">Say Hello</a>
</body>
</html>

Web Pages/WEB-INF/jsp/hello.jsp:

<html>
<head>
    <title>Spring 3.0 MVC Series: Hello World - ViralPatel.net</title>
</head>
<body>
    ${message}
</body>
</html>

So when i launch the appication the index.jsp is loaded correctly but when i click on the href to navigate to hello.jsp i got a 404 error and the server log says:

No mapping found for HTTP request with URI [/Elso/hello.html] in DispatcherServlet with name 'spring'

I've checked out dozens of articles like that, but I just can't find the mistake, anybody has any idea what could it be?

Spring Solutions


Solution 1 - Spring

Add

  <mvc:default-servlet-handler/>

to spring-servlet.xml

Solution 2 - Spring

You could try and add an @Controller annotation on top of your myController Class and try the following url /<webappname>/my/hello.html. This is because org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping prepends /my to each RequestMapping in the myController class.

Solution 3 - Spring

If you are using

<mvc:annotation-driven/> 

make sure your spring-servlet.xml has correct

<context:component-scan base-package="com.....controller" /> tag. 

Basically, you need to include all the packages where you have used the annotation in your java code.

Also, please ensure you do not have duplication of component-scan (for a discovery of beans). If your config XML already contains the element, then any of your Controller classes that are annotated with @ComponentScan(basePackages=... needs to be stripped of the said annotation.

Solution 4 - Spring

I solved my issue with : Java Build Path -> JRE system library - > Edit -> Alternate JRE ->

Solution 5 - Spring

Make sure

<mvc:annotation-driven/>
<context:component-scan base-package="com.hireartists.web.controllers"/>

points to proper package that contains controllers.

Solution 6 - Spring

Please check your [PROJECT_NAME]\target\classes directory to see whether myController.class is generated or not.

If not, please check all your java source code whether there are any compilation errors.

Solution 7 - Spring

Try:

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Worked for me!  

Solution 8 - Spring

If you are using Java code based on Spring MVC configuration then enable the DefaultServletHandlerConfigurer in the WebMvcConfigurerAdapter object.

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 
		configurer.enable();
}

Solution 9 - Spring

Check ur Bean xmlns..

I also had similar problem, but I resolved it by adding mvc xmlns.

<?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:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">


	<context:component-scan base-package="net.viralpatel.spring3.controller" />
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

Solution 10 - Spring

addition of <mvc:annotation-driven/> worked for me. Add it before line <context:component-scan ............/>

Solution 11 - Spring

If you want to serve .html files, you must add this <mvc:default-servlet-handler /> in your spring config file. .html files are static. Hope that this can help someone.

Solution 12 - Spring

It is not finding the controllers, this is basic issues. it can be due to following reasons.

A. inside WEB-INF folder you have file web.xml that refers to dispatcherServlet. Here it this case is mvc-config.xml

<servlet>
	<servlet-name>dispatcherServlet</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/mvc-config.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>

B. This mvc-config.xml file have namespaces and it has to scan the controllers.

<context:component-scan base-package="org.vimal.spring.controllers" />
<mvc:annotation-driven />

C. Check for the correctness of the package name where you have the controllers. It should work.

All Controllers must be Annotated with @Controller.

Solution 13 - Spring

Had the exact same error and it took me a long time trying to understand it. It is most likely down to compilation errors, the java classes did not get published in your servlet. Please check this by going in the server that you are using \tmp1\wtpwebapps[PROJECT_NAME]\WEB-INF\classes\ and try to find you controller classes to see whether or not they have been published. If not you need to get to the bottom of any compilation errors.

Solution 14 - Spring

If you are using maven as build tool for project , build your project properly,your changes in the code and xml files are not reflecting after compilations.

Solution 15 - Spring

If you depend on Spring Social, check that you have configured a Web Controller bean:

import org.springframework.context.annotation.Bean;
import org.springframework.social.connect.web.ConnectController;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionRepository;

...

@Bean
public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) {
	return new ConnectController(connectionFactoryLocator, connectionRepository);
}

Solution 16 - Spring

if you are using maven then do run maven install command before you run your web app on a server as it will generate a class file for your controller and in my experience that is what your application has been missing.

Solution 17 - Spring

I had the same issue and after lots of reserach I found the classes were not getting published in my target folder. So I had run the below two commands from cmd

  1. mvn clean install
  2. mvn package

Surprisingly I was able to access the page and error was gone. Same can be verified from target folder where you will be able to find the complied classes which were missing earlier.

Solution 18 - Spring

Add @Controller to your controller or where ever you have the @RequestMapping for you json end point.

This worked for me while deploying a similar application.

Solution 19 - Spring

If you are using Maven , Add these to your pom.xml

<dependency>
<groupid>javax.servlet</groupid>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>

<dependency>
<groupid>taglibs</groupid>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<scope>runtime</scope>

Solution 20 - Spring

I also faced the same issue, but after putting the namespace below, it works fine:

xmlns:mvc="http://www.springframework.org/schema/mvc" 

Solution 21 - Spring

Removing the Tomcat Server and adding new tomcat configuration in Eclipse resolved issue for me.

Solution 22 - Spring

In pom.xml make sure packaging is set to war like <packaging>war</packaging> ,not to jar or any thing else.

Solution 23 - Spring

What is /Elso?

You try:

@RequestMapping("/Elso")
public class myController {

    @RequestMapping("/hello")
    public ModelAndView helloWorld() {
    
        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello", "message", message); 
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionerik.cView Question on Stackoverflow
Solution 1 - SpringHarryView Answer on Stackoverflow
Solution 2 - SpringNilsView Answer on Stackoverflow
Solution 3 - SpringsskView Answer on Stackoverflow
Solution 4 - SpringacgView Answer on Stackoverflow
Solution 5 - SpringprayagupaView Answer on Stackoverflow
Solution 6 - SpringkishoreView Answer on Stackoverflow
Solution 7 - SpringUdeep ShakyaView Answer on Stackoverflow
Solution 8 - SpringGaurav GoyalView Answer on Stackoverflow
Solution 9 - SpringVishal GuptaView Answer on Stackoverflow
Solution 10 - SpringNilay DeshmukhView Answer on Stackoverflow
Solution 11 - SpringAleeView Answer on Stackoverflow
Solution 12 - Springvimal krishnaView Answer on Stackoverflow
Solution 13 - SpringJanView Answer on Stackoverflow
Solution 14 - SpringnatView Answer on Stackoverflow
Solution 15 - SpringnaXa stands with UkraineView Answer on Stackoverflow
Solution 16 - SpringAshish JaggaView Answer on Stackoverflow
Solution 17 - SpringanshuView Answer on Stackoverflow
Solution 18 - SpringVineela ThonupunuriView Answer on Stackoverflow
Solution 19 - SpringMedini PatkiView Answer on Stackoverflow
Solution 20 - SpringRavikumar D GView Answer on Stackoverflow
Solution 21 - SpringD8SasiView Answer on Stackoverflow
Solution 22 - SpringHadi RasouliView Answer on Stackoverflow
Solution 23 - SpringDalton DiasView Answer on Stackoverflow