-2

I am a newbie to spring mvc framework and I have the following problem. So when I run the code underneath on the server and then typing the following url http://localhost:8080/FirstSpringMVCProject/welcome in the browser, I am getting the error 404 error rendered and nothing else.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>FirstSpringMVCProject</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

<?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: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"> 

    <bean id="HandlerMapping"
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

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

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


    </bean>


</beans>

HelloPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<!-- <title>Insert title here</title> -->
</head>

<h1> First Spring MVC Application Demo </h1>

<h2>${msg}</h2>

<body>

</body>
</html>

HelloController

package com.stack;

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 model = new ModelAndView("HelloPage");
        model.addObject("msg", "hello world!");

        return model;
    }

}

The URL which I am typing in the browser:

http://localhost:8080/FirstSpringMVCProject/welcome

After typing the URL in the browser I am getting the following in the console:

Jun 20, 2016 7:15:15 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNUNG: No mapping found for HTTP request with URI [/FirstSpringMVCProject/welcome] in DispatcherServlet with name 'spring-dispatcher'

screen shoot

enter image description here

TheBook
  • 1,698
  • 1
  • 16
  • 32
  • Where do you keep HelloPage.jsp? You need to keep it in WEB-INF as per your view resolver. – Vijendra Kumar Kulhade Jun 20 '16 at 17:32
  • @VijendraKulhade: Yes I am keeping it there. Please take a look at my screen shoot which I added just now. – TheBook Jun 20 '16 at 17:35
  • 1
    What do you think `BeanNameUrlHandlerMapping` does? Why did you register that as a bean? What part of your app do you think should serve the path `/welcome` and why do you think so? – Sotirios Delimanolis Jun 20 '16 at 17:45
  • @SotiriosDelimanolis: BeanNameUrlHandlerMapping is the default handler mapping mechanism, which maps URL requests to the name of the beans. And I registered it as a bean because the guy in the tutorial registed it in this way. `HelloPage.jsp`is the welcome page because the `helloWorld` method has `@RequestMapping("/welcome")` as a notation. Please can u tell me how to solve the problem? – TheBook Jun 20 '16 at 17:57
  • Best to start with the javadoc of the class you use. Here's the one for [`BeanNameUrlHandlerMapping`](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/handler/BeanNameUrlHandlerMapping.html). Notice how it says _Implementation of the `HandlerMapping` interface that map from URLs to beans with names that start with a slash ("/"), similar to how Struts maps URLs to action names._ You don't have any such beans. So nothing is registered to handle any paths within your Spring MVC stack. – Sotirios Delimanolis Jun 20 '16 at 18:08
  • I tried to run your files in my spring project and it worked for me. I am runnin g it on tomcat7 maven plugin. Also I tried with spring 4.2.6. If you want to try with tomcat7 plugin add ` org.apache.tomcat.maven tomcat7-maven-plugin 2.2 ` in your pom. and run `mvn tomcat7:run` – Vijendra Kumar Kulhade Jun 20 '16 at 18:20
  • @VijendraKulhade: I am working with eclispe `Version: Mars.2 Release (4.5.2) + java 8 + Tomcat 8` but I solved it after adding the following `` after the `BeanNameUrlHandlerMapping` in the `spring-dispatcher-servlet.xml` file. – TheBook Jun 20 '16 at 18:30

1 Answers1

-1

Please add just

<load-on-startup>1</load-on-startup> in web.xml. This will intialize spring.

Also add

<mvc:annotation-driven /> 

in spring-dispatcher-servlet.xml to support Spring mvc message converters like Jackson

Vijendra Kumar Kulhade
  • 2,217
  • 3
  • 15
  • 25
  • This solution was right and I tried replicating whole scenario in my sample project and fixed it like this. As there was no `1` for dispatcher servlet Spring context was not loading at the startup. – Vijendra Kumar Kulhade Jun 21 '16 at 15:27
  • `load-on-startup` with a value of 0 (the default) will still load the context, except it will do it lazily, when the first request for the corresponding `servlet` arrives. This cannot be the issue. The intention of the tutorial is to use `BeanNameUrlHandlerMapping`. Although the `annotation-driven` will register the `@Controller` bean, it's not doing what the tutorial is teaching. Also, I don't see why you're mentioning the converters. The main purpose of `annotation-driven` is to setup the handling stack with `RequestMappingHandlerMapping`. – Sotirios Delimanolis Jun 21 '16 at 16:39