74

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?

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
erik.c
  • 1,342
  • 2
  • 15
  • 27

23 Answers23

71

Add

  <mvc:default-servlet-handler/>

to spring-servlet.xml

Harry
  • 4,705
  • 17
  • 73
  • 101
40

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nils
  • 1,750
  • 14
  • 10
31

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.

Hyyan Abo Fakher
  • 3,497
  • 3
  • 21
  • 35
ssk
  • 431
  • 4
  • 4
  • I added this. But I get the error 'The prefix "mvc" for element "mvc:annotation-driven" is not bound.' – Hari Ram Sep 05 '15 at 02:08
  • This was it for me - we inherited the project and the parameter for the package names had not been updated to the new codebase. – J E Carter II Dec 07 '16 at 15:12
15

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

As it was configured to JDK folder so it was giving Exception

acg
  • 179
  • 3
  • 13
12

Make sure

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

points to proper package that contains controllers.

prayagupa
  • 30,204
  • 14
  • 155
  • 192
10

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.

Sampada
  • 2,931
  • 7
  • 27
  • 39
kishore
  • 137
  • 2
9

Try:

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

Worked for me!  

Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
Udeep Shakya
  • 519
  • 4
  • 4
9

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();
}
Gaurav Goyal
  • 91
  • 1
  • 2
4

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>
davegson
  • 8,205
  • 4
  • 51
  • 71
3

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

3

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.

Alee
  • 740
  • 7
  • 19
3

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.
vimal krishna
  • 2,886
  • 28
  • 22
2

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.

Jan
  • 21
  • 1
2

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.

nat
  • 557
  • 2
  • 11
  • 25
2

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);
}
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
2

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.

Ashish Jagga
  • 189
  • 3
  • 6
  • 10
2

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.

anshu
  • 51
  • 5
2

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.

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
1

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>

  • Welcome to SO, and thanks for your answer. This question has been flagged for review because of its brevity. It would help if you could expand the question to include an explanation of the problem the OP is having and why your solution helps. – Software Engineer May 27 '14 at 14:28
1

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

xmlns:mvc="http://www.springframework.org/schema/mvc" 
AJPerez
  • 3,435
  • 10
  • 61
  • 91
1

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

D8Sasi
  • 79
  • 6
1

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

Hadi Rasouli
  • 1,871
  • 3
  • 27
  • 43
0

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); 
    }
}
Dalton Dias
  • 76
  • 1
  • 4
  • 9