sameer thakur
sameer thakur

Reputation: 11

Spring mvc Component scan requestmapping not working

I am new to Spring and Spring-MVC. I am using 4.3.3.RELEASE.I have generated project using maven-webapp-archetype and added sping dependencies and dispatcher-servelt later. As per my web.xml I have initialized Dispatcher-servlet using spring-dispatcher-servlet.xml

I have already tried

  1. Added inet.controller.* instead inet.controller in base-package
  2. Changing URL pattern to /* from /
  3. Adding to dispatcher servlet

but still when I try http://loclahost:8080/inet/welcome server is throwing 404.

Web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

Spring-dispatcher-servlet.xml

<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-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <mvc:default-servlet-handler />
    <context:component-scan base-package="inet.controller"/>
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

Below is my directory structure:

enter image description here

HelloController

package inet.controller;

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

@Controller
public class HelloController {

    @RequestMapping("/welcome")
    public ModelAndView helloWorld() {
        ModelAndView modelAndView = new ModelAndView("helloPage");
        modelAndView.addObject("message", "Hi All");
        return modelAndView;
    }
}

Upvotes: 1

Views: 1795

Answers (1)

kuhajeyan
kuhajeyan

Reputation: 11017

You need to tell the spring how to find the controllers from urls that you are hitting. So it what is missing in your configuration

to do that add <mvc:annotation-driven/> tag in your dispatcher xml configuration. it adds some mapping handlers

  1. RequestMappingHandlerMapping
  2. RequestMappingHandlerAdapter
  3. ExceptionHandlerExceptionResolver

And some necessary message converters for you.

What does <mvc:annotation-driven /> do?

Upvotes: 2

Related Questions