user1189577
user1189577

Reputation: 1

Spring MVC annotations configuration

I am trying to create a simple spring mvc application for practice but I keep getting this error:

No mapping found for HTTP request with URI in DispatcherServlet with name 'mvc-dispatcher' and I get the 404 error from Tomcat.

This is my mvc-dispatcher-servlet.xml

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

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<mvc:annotation-driven />

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

<bean
    id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" >

    <property
        name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />

    <property
        name="suffix"
        value=".jsp" />
</bean>

</beans>

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">  
<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>*.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
   <listener-class>
      org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>

</web-app>

And this is my controller

package controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/welcome.do")
public class HelloWorldController{

@RequestMapping(method=RequestMethod.GET)
public ModelAndView helloWorld(){
    System.out.println("**Hit controller**");
    ModelAndView model = new ModelAndView("spring");
    model.addObject("msg", "hello world");
    return model;
}
}

I could get the controller to work when using beans in the dispatcher servlet but for some reason I cannot get the correct handler mapping with annotations. Am I guessing that there is something in my mvc-dispatcher-servlet.xml file that is messing things up and any help would be great.

Upvotes: 0

Views: 2319

Answers (3)

kapitanrum
kapitanrum

Reputation: 180

Spring configuration is setted for parent spring context.

MVC will work only with ServletDispatcher:

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">  
<servlet>

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

<servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

<listener>
   <listener-class>
      org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>

</web-app>

Upvotes: 0

ravi ranjan
ravi ranjan

Reputation: 6129

First cross check your package name with the following

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

There might be a chance that it is incorrect, base-package means full package path like "org.self.etc"

If it is correct then put this line above <mvc:annotation-driven />

Upvotes: 0

chaldaean
chaldaean

Reputation: 1232

Your "mvc-dispatcher-servlet.xml" looks correctly, It works for me. Make sure that you use correct url. And you can look at this simple example Spring MVC Hello World Annotation Example

Upvotes: 0

Related Questions