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

StackOverflow https://stackoverflow.com/questions/22151491

  •  19-10-2022
  •  | 
  •  

Pregunta

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>

No hay solución correcta

Otros consejos

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";
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top