Abhishek
Abhishek

Reputation: 19

Request is not mapped to the controller

When I go with the url..http://localhost:8080/springdemo/hello..it is showing 404 not found error..I have put my java file inside src/main/java as usual in Maven project. My controller code is as follows :-

package org.abhishek;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.lang.System;

@Controller
public class HelloWorldController {
    @RequestMapping(value="/hello", method = RequestMethod.GET)
    public ModelAndView helloWorld() {
        System.out.println("hello**");
        String message = "Hello World, Spring MVC @ Javatpoint";
        return new ModelAndView("hello", "message", message);
    }
}

Web.xml file given below

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="java.sun.com/xml/ns/javaee"; xmlns:xsi="w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="java.sun.com/xml/ns/javaee java.sun.com/xml/ns/javaee/web-app_2_5.xsd">;
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
</web-app>

I put this line System.out.println() for debugging purpose and I found that this method is not executed with the above mentioned url...i.e. http://localhost:8080/springdemo/hello.Please answer..thanx in advance.

Upvotes: 0

Views: 840

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522541

This is speculative, but you could have a mapping problem in your web.xml file, which would result in the Spring controller not even being hit (despite having a correct @RequestMapping annotation). Your web.xml file should have the following servlet mapping:

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

<servlet-mapping>
    <servlet-name>springServlet</servlet-name>
    <url-pattern>/springdemo/*</url-pattern>
</servlet-mapping>

Now paste your URL into a web browser and see if you can hit it:

http://localhost:8080/springdemo/hello

Upvotes: 1

Related Questions