FrankelStein
FrankelStein

Reputation: 990

No mapping found for HTTP request with URI xxx in DispatcherServlet with name xx

I looked up in almost every topic I found concerning that error but none of them was useful to me.

I created a simple Spring MVC project, when I run the project I get this error :

févr. 19, 2016 12:29:56 PM org.springframework.web.servlet.PageNotFound noHandlerFound
AVERTISSEMENT: No mapping found for HTTP request with URI [/SpringMVCsample1/] in DispatcherServlet with name 'one'

Here is my code :

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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringMVCsample1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>jsp/hello.jsp</welcome-file>
  </welcome-file-list>



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

   <servlet>
      <servlet-name>one</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

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

   </web-app>

one-servlet.xml :

<?xml version="1.0" encoding="UTF-8"?>

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

    <!-- This will allow Spring to load all the components from package com.capgemini.springtest   -->
    <!-- and all its child packages. -->
   <context:component-scan base-package="com.capgemini.springtest" />

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

</beans>

OneController.java :

package com.capgemini.springtest;


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


@Controller
public class OneController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String printHello(ModelMap model) {

        model.addAttribute("message", "Hello Spring MVC Framework!");

        return "hello";
    }

}

hello.jsp :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
   <h2>${message}</h2>
</body>
</html>

Any ideas ?

Upvotes: 4

Views: 1677

Answers (4)

tkralik
tkralik

Reputation: 850

Try adding <mvc:annotation-driven /> into the one-servlet.xml. Remember to add the definition of the mvc namespace there as well xmlns:mvc="http://www.springframework.org/schema/mvc"

Then try to access localhost:8080/SpringMVCsample1/hello. Not localhost:8080/SpringMVCsample1/hello/ like you said you have tried in one of the comments

Upvotes: 0

Kunal Surana
Kunal Surana

Reputation: 659

Change url-pattern in web xml :

<url-pattern>/*</url-pattern>

Change Controller to have mapping of root context:

package com.capgemini.springtest;


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


@Controller
public class OneController {

    @RequestMapping(value = {"/","/hello"}, method = RequestMethod.GET)
    public String printHello(ModelMap model) {

        model.addAttribute("message", "Hello Spring MVC Framework!");

        return "hello";
    }

}

hello.jsp should reside under folder : /WEB-INF/jsp/hello.jsp

Upvotes: 2

Jeff Morin
Jeff Morin

Reputation: 1010

In web.xml your servlet is mapping the / URI only. The URL pattern should rather be /*.

Upvotes: 0

achabahe
achabahe

Reputation: 2565

of course you are going to have such a problem because you don't have any controller to deal with request to "/" what you have to do is to add a controller or add index.jsp under you web folder

web
|--->WEB-INF
|    |--->jsp
|        |---->index.jsp
|--->index.jsp

Upvotes: 1

Related Questions