0

i am new to Spring technology. I created a Spring MVC project when i try to run project using Tomcat i am getting following error in eclipse console. Please help me out, i tried several options by searching in Google but clueless. I don't know where went wrong...

Error:

org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/controller/] in DispatcherServlet with name 'SpringMVC'

HomeController.java

@Controller
  public class HomeController {

@RequestMapping(value = "/student", method = RequestMethod.GET)
   public ModelAndView student() {
      return new ModelAndView("student", "command", new Student());
   }

   @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
   public String addStudent(@ModelAttribute("SpringWeb")Student student, 
   ModelMap model) {
      model.addAttribute("name", student.getName());
      model.addAttribute("age", student.getAge());
      model.addAttribute("id", student.getId());

      return "result";
   }

  }

web.xml

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/root-context.xml</param-value>
 </context-param>

 <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/SpringMVC-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
   </servlet>

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

SpringMVC-servlet.xml

  <mvc:annotation-driven />
  <context:component-scan base-package="com.spring.controller" />
  <resources mapping="/resources/**" location="/resources/" />
  <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>
Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
  • 1
    Why do you think `/controller/` should invoke your controller? You have two mappings, one for `/student` and one for `/addStudent`. – JB Nizet Mar 03 '14 at 16:11
  • You don't have any @RequestMapping for HomeController. Perhaps you should have something like @RequestMapping(value="/controller") before the HomeController class. – helderdarocha Mar 03 '14 at 16:13
  • JB Nizet- You are right i mentioned two mappings but my question from where it tooks "/controller/" i didnt mention anything like /contoller/ in code...my final URL looks like localhost:port/controller/...please clarify it... – Senthil Kumar Mar 03 '14 at 16:36
  • helderdaroch - i added requestmapping before HomeController class but still same error... :( – Senthil Kumar Mar 03 '14 at 16:38
  • Because you haven't mapped any controller to */controller/*. Do you know how *@Requestmapping* works? – Bart Mar 03 '14 at 16:41

1 Answers1

-1

You have no mapping for controller. Now you have only /student as GET and /addStudent as POST. Even if you add @RequestMapping("/controller") to your controller class it will not work because there is still no mapping for /controller. As you can see mapping will be for /controller/student and /controller/addStudent. I recomend you to change requesting URL to just /student and /addStudent or if you need this /controller URL you should have additional method looks like below

@RequestMapping("controller")
public String anyMethodName(Model model) {
    //code goes here
    return "anythink";
}
shark
  • 995
  • 1
  • 8
  • 19