No mapping found for HTTP request with URI [/myproject/] in DispatcherServlet with name 'appServlet'

23.8k views Asked by At

I'm absolutely newbie with Java and Spring and I want to learn from example.

I'm using an out-of-the-box configuration / installation

  • Mac OSX
  • Springsource Tool Suite as IDE
  • Spring 2.8.1.RELEASE
  • vfabric-tc-server-developer-2.6.1.RELEASE

I've tried to generate a new project based on "Spring Template Project". Then I've chosen the "Spring MVC Project". The sample project is generated. After that, without modifying anything, I've tried to execute de "home.jsp" page via "Run As". The Web Server starts and finally I received the error in the console Tab.

No mapping found for HTTP request with URI [/myproject/] in DispatcherServlet with name 'appServlet'

And this other output in these web pages:

  • http://localhost:8080/myproject/WEB-INF/views/home.jsp
  • http://localhost:8080/myproject

enter image description here

Here you can see an image on how my project is structured (auto generated for STS):

enter image description here

What is wrong?

Here you can see the content of the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

The root-context.xml file has this content.

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

    <!-- Root Context: defines shared resources visible to all other web components -->

</beans>

And finally the servlet-context.xml has this content.

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

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.mycompany.myapp" />

</beans:beans>

Does anybody have an idea to solve it?

3

There are 3 answers

2
JB Nizet On

Everything under WEB-INF is not accessible from the outside, and the point of an MVC framework is to invoke a controller before dispatching to the view, so invoking the JSP directly should not be done anyway.

And you probably don't have any Spring controller mapped to the root URL, so obviously, there is nothing at the URL http://localhost:8080/myproject/.

1
duffymo On

Spring's convention is to assume that the <servlet-name> in the web.xml for the DispatcherServlet matches the beginning of the Spring servlet context XML file. Rename servlet-context.xml to appServlet-context.xml and see if that helps.

0
Alonso Dominguez On

Add a Controller to your application that returns a ModelAndView instance with name "home". Then configure some handler mapping to that Controller and try to access your web app with a URL similar to this one: http://localhost:8080/myproject/home.do. More information can be found here and here.