0

I am trying a simple Spring Annotation Example from A Tutorial Site , but When i hit the URL with the specified URI then I am getting this error , Searched on net found many solutions but none of them worked

  • Below is the code snippet :

Web.xml

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>  
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

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

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

spring-servlet.xml

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

    <context:component-scan base-package="com.spring.annotation" />

        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

LoginController.java

    package com.spring.annotation;

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;

@Controller
public class LoginController {

    @RequestMapping("/hello")
    public ModelAndView login(){
        System.out.println("Inside login Controller");
        String message="Welcome in the Spring MVC Annotaion ";
        return  new ModelAndView("default","message",message);  
    }
}

Now when i run this program in Browser

http://localhost:8080/SpringAnnotation/hello

I am getting no error but simple warning and i am stuck :

Jan 19, 2015 3:10:06 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/SpringAnnotation/hello] in DispatcherServlet with name 'spring'
Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62

4 Answers4

0

I didn't see a <mvc:annotation-driven> in your spring-servlet.xml. Did you try to add this ? This tag is used to make the link between the scanned packages and the annotation @Controller

vincent
  • 1,214
  • 12
  • 22
0

You are missing MVC configuration in your spring-context.xml. It should look something like this

<?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:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.spring.annotation" />

    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

You were missing mvc namespace declaration, plus <mvc:annotation-driven /> to let Spring know you are using annotations for configuration.

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
  • Yes I already added that but still the same warning – Neeraj Jain Jan 19 '15 at 10:37
  • What if you add also ``? – Predrag Maric Jan 19 '15 at 10:49
  • Added Now the Warning on the console is gone but still web browser showing **HTTP Status 404 - /SpringMavenAnnotation/hello** – Neeraj Jain Jan 19 '15 at 10:58
  • In the question you were trying to access `SpringAnnotation`, not `SpringMavenAnnotation`? Anyway, is the controller method getting called at all? You would also get 404 if it is called, but `default.jsp` couldn't be found in configured location. – Predrag Maric Jan 19 '15 at 11:04
  • Yes it is SpringAnnotation sorry for that , and jsp is at it's perfect location , if it would have gone untill controller it must print out the statement into it but i think xml not able to redirect to the perfect controller – Neeraj Jain Jan 19 '15 at 11:08
  • Ok, now try to change servlet mapping in `web.xml` to `/*` – Predrag Maric Jan 19 '15 at 11:46
0
You have specified @RequestMapping wrong way try this way 
@RequestMapping(value = "/home", method = RequestMethod.GET)

Also its may be due to wrong url ,may be your application name is wrong 
To know your application name go to pom.xml and see this entry :

        <modelVersion>4.0.0</modelVersion>
        <groupId>com.test</groupId>
        <artifactId>SpringTest</artifactId>
        <name>SpringAnnotation</name>
        <packaging>war</packaging>

  name of application in this case is SpringTest and related url is /SpringTest/home. Use same thing in your project.
0

Changed Maven Project to Normal Dynamic Web Project .

Everything Remains the same , Even Removed <mvc:default-servlet-handler /> and <mvc:annotation-driven/> tag from spring-servlet.xml . Now Working Perfectlly fine .

Now my question is , wheather pom.xml is the root cause for the warning coming ?

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62