0

When I'm trying to run my spring web application I am getting the following error:

org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/HibwenateWeb/index] in DispatcherServlet with name 'customerdispatcher

I am not able to load my home page. I gone through many articles releavent to the error, mostly every one is suggested to add <mvc:default-servlet-handler /> in dispatcher after adding that I am not getting that nohandler error but still the home page is not loading. I coudn't figure out the issue..Can any one help me on this?

My controller

package com.springforbeginners.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CustomerController {
    @RequestMapping(value="/index")
    public String listCustomers(ModelAndView mav) {
        System.out.println("Hi i am in controller");
        //map.put("customer", new Customer());
        //map.put("customerList", customerService.listCustomer());
        mav.setViewName("customer");
        return "customer";
    }

}

customerdispatcher-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:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx" 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.1.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.1.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
         http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <mvc:annotation-driven />
    <context:annotation-config />
    <context:component-scan base-package="com.springforbeginners" />

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

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>customerdispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>customerdispatcher</servlet-name>
        <url-pattern>/index</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

</web-app>
Valijon
  • 12,667
  • 4
  • 34
  • 67
Mohan
  • 1
  • 1
  • 1

2 Answers2

0

I wrote a very simple prj in your structure, here it is. pom dependencies

<dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

web.xml under WEB-INF

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>MyFirstSpringMVC</display-name>
    <servlet>
        <servlet-name>customerdispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>customerdispatcher</servlet-name>
        <url-pattern>/index/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

customerdispatcher-servlet.xml under WEB-INF

<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.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <context:component-scan base-package="com.*"/>
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <!-- <mvc:annotation-driven /> --> 
</beans>

customer.jsp under /WEB-INF/jsp/

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
this is customer.jsp
</body>
</html>

CustomerController

package com.springforbeginners.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CustomerController {

    @RequestMapping(value="/index")
    public String listCustomers(ModelAndView mav) {
        System.out.println("Hi i am in controller");
        return "customer";
    }
}

This is the result. https://i.stack.imgur.com/LaGEd.jpg

Since you declare your customerdispatcher-servlet.xml in default way("dispatcher name"+"-servlet.xml"), so you don't need to add spring contextloader or set init-param to make the dispatcher servlet work.

Further info http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-servlet

Hope this can help you.


By the way, when declaring the url-pattern to the root path

    <servlet-mapping>
        <servlet-name>customerdispatcher</servlet-name>
        <url-pattern>/</url-pattern> <= it's slash only, not /*
    </servlet-mapping>

this subtle difference have made me an hour to find it...

Duncan
  • 696
  • 1
  • 5
  • 15
  • I changed all my files as you suggested but still i'm getting only 404 error..(i.e no handler found)..Is it because of any spring jar version?I'm using spring 3.1.1 version..Could you please help me to fix the issue? – Mohan Mar 21 '16 at 17:28
0

You have to add the following elements to your web.xml.

<!-- The definition of the Root Spring Container -->
<context-param>  
    <param-name>contextConfigLocation</param-name> 
    <!-- provide the path for your customerdispatcher-servlet.xml -->
    <param-value>/WEB-INF/customerdispatcher-servlet.xml</param-value>  
</context-param>  

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>  

Furthermore, you have to change the url-pattern element to:

<url-pattern>/</url-pattern>
nnunes10
  • 550
  • 4
  • 14