0

I am a spring beginner, I used controller and request mapping to go to some java file from index.jsp but its showing 404 Error. I am putting right urlmapping and requestmapping and controllers as I saw in a spring tutorial. I have tried including more dependencies, changing the code as I saw someplace else, but nothing working. Please help me with this code. Thanks in Advance

index.jsp:

    <html>
    <body>
    <form action="add">
    <input type="text" name="t1"><br>
    <input type="text" name="t2"><br>
    <input type="submit">
    </form>
    </body>
    </html>

web.xml:

    <!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>bhoomika</servlet-name>
      <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      </servlet>
      <servlet-mapping>
      <servlet-name>bhoomika</servlet-name>
      <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>

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

    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="garg.bhoomika"></context:component- 
     scan>

    </beans>

AddController.java

    package garg.bhoomika;

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

    @Controller
    public class AddController 
    {
    @RequestMapping("/add")
    public void add()
    {
        System.out.println("I am here");
    }

    }

Error:

org.springframework.web.servlet.PageNotFound noHandlerFound WARNING: No mapping found for HTTP request with URI [/SpringMVC/add] in DispatcherServlet with name 'bhoomika'

vahdet
  • 6,357
  • 9
  • 51
  • 106

1 Answers1

0

For now you should use <mvc:annotation-driven/> in your spring confuguration file to enable MVC configuration as described in official doc
Try to replace entry <context:annotation-config> to <mvc:annotation-driven/>.
And if you want to go to your index.jsp you should change method add() so that it returned view name as a String:

 @RequestMapping("/add")
    public String add() {
        return "index";
    }

Also you can see the exhaustive and descriptive answer to similar question this. It'll be useful.

Anthony
  • 571
  • 3
  • 11