Search code examples
javaxmlspringspring-mvcspring-annotations

WARNING: No mapping found for HTTP request with URI [/mvc/add] in DispatcherServlet with name 'dispatcherservlet'


I saw similar questions and tried maping URL from /* to / and it dosnt work. I also tried hitting the URL directly from browser, and it too doesn't work. im new to Spring can someone help me here.

    <!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>dispatcherservlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value></param-value>
    </init-param> -->
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcherservlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

enter image description here

and my dispatcherservlet-servlet.xml is

    <?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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">

    <mvc:resources mapping="/style/**" location="/style/"/>
<mvc:default-servlet-handler/>

<context:annotation-config/> 
   <context:component-scan base-package="com"/>
</beans>

finally my controller

    package com;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class addition {

    @RequestMapping("/add")
 void add() {
     System.out.println("im addint");
 }
}

my index.jsp as welcome file

 <html>
<body>
<h2>Hello World!</h2>
<form action="add">
<input type="text" name="first number">first number<br>
<input type="text" name="second number">second number<br>
<input type="submit" name="add two numbers"><br>

</form>
</body>
</html>

when I run I can submit and hit the URL for add method but im getting WARNING: No mapping found for HTTP request with URI [/mvc/add] in DispatcherServlet with name 'dispatcherservlet'


Solution

  • You are missing <mvc:annotation-driven /> on dispatcherservlet-servlet.xml, which enables @Controller annotation.