alvaro991
alvaro991

Reputation: 67

Spring MVC - No mapping found for HTTP request

In my work I have a realy big problem with Spring MVC. I have downloaded sample project from our SVN. It contains simple Spring project structure with Maven. Sample project works just fine. Tomcat starts and I see beautiful "Hello World" page (from index.jsp) on http://localhost:8080/XXX/.

The problem is, that I want to write a controller to handle for example "/welcome".

So I have written servlet (mvc-dispatcher-servlet.xml), updated my web.xml file, added controller. But every time I write "http://localhost:8080/XXX/welcome" I get

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

here is my configuration

mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">

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

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

</beans>

web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>

    <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>

    <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>

HelloWorldController.java

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

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

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        model.addAttribute("message", "Spring 3 MVC Hello World");
        return "hello";
    }

Here is my project structure (for me it's quiet different from what I am used to, but it not depends on me). http://postimg.org/image/p649rxbjr/

I guess, that the problem is in web.xml (servlet conf), but after searching google i have no working solution. Hope you help!

Upvotes: 1

Views: 3129

Answers (2)

carlbarc
carlbarc

Reputation: 1

I had the same issue and I tried everything. What finally worked was not including the XXX part but just http://localhost:8080/welcome

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279880

You haven't declared

<mvc:annotation-driven />

in your context (and the corresponding namespace). As such, Spring doesn't register any handlers with your DispatcherServlet. You will need to add it.

Upvotes: 2

Related Questions