1

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;
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Why are U using xml confings ?) It's 2016. I guess you've missed `` in Spring-dispatcher-servlet.xml – ikos23 Oct 23 '16 at 09:05
  • If you're new to Spring, I recommend skipping the legacy configuration and starting with Spring Boot. You can have it autogenerate you a Maven project with [Spring Initializr](https://start.spring.io) that eliminates the need for any of the XML mappings. – chrylis -cautiouslyoptimistic- Oct 23 '16 at 09:21
  • I would first try to rename "spring-dispatcher" to say "springDispatcher". also show your web-inf subfolder structure.tree – AchillesVan Oct 23 '16 at 10:25
  • try to add this in your Spring-dispatcher-servlet.xml right before your – AchillesVan Oct 23 '16 at 11:06
  • @Georgesvanhoutte added but that dint help – sameer thakur Oct 23 '16 at 11:27
  • 1
    what if you browse to http://loclahost:8080/welcome – AchillesVan Oct 23 '16 at 11:42
  • @Georgesvanhoutte still 404 – sameer thakur Oct 23 '16 at 11:44
  • sameer thakur your index.jsp is in your web-inf folder right ? – AchillesVan Oct 23 '16 at 11:59
  • @Georgesvanhoutte My index.jsp is at same levelas web-inf it works fine but when I try localost:8080/inet/welcome I see below in logs Oct 23, 2016 5:32:32 PM org.springframework.web.servlet.PageNotFound noHandlerFound WARNING: No mapping found for HTTP request with URI [/inet/welcome] in DispatcherServlet with name 'spring-dispatcher' – sameer thakur Oct 23 '16 at 12:04
  • I know. Just move your index.jsp to your web-inf folder according to the way you have configured your InternalResourceViewResolver inside your Spring-dispatcher-servlet.xml – AchillesVan Oct 23 '16 at 12:10
  • Usualy the "noHandlerFound " error means that the dispatcher servlet cannot locate your @controller class. Thats why i priorly suggetsed the – AchillesVan Oct 23 '16 at 12:12
  • @Georgesvanhoutte now not even welcome page is opening . I am getting 404 for http://localhost:8080/inet/ – sameer thakur Oct 23 '16 at 12:13
  • Try this similar tutorial: https://www.tutorialspoint.com/spring/spring_mvc_form_handling_example.htm – AchillesVan Oct 23 '16 at 12:15
  • You are requesting `/inet/welcome` while your mapping is `/welcome`. Unless your application is deployed on `/inet` it will not map. – M. Deinum Oct 23 '16 at 17:38

1 Answers1

2

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?

Community
  • 1
  • 1
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71