Search code examples
javaspringspring-mvchttp-status-code-404spring-annotations

Spring : Unable to resolve view name in Spring MVC


I do have gone through some solutions, none has work in my case. I have been getting "Not Found" error on browser on running this simple spring mvc example. I am new to Spring framework. Any help would be highly appreciated.

Following is the error I get on Console when I run the program :

WARN [org.springframework.web.servlet.PageNotFound] (default task-4) No mapping found for HTTP request with URI [/Demo1/] in DispatcherServlet with name 'SpringDispatcher' WARN [org.springframework.web.servlet.PageNotFound] (default task-5) No mapping found for HTTP request with URI [/Demo1/HelloPage] in DispatcherServlet with name 'SpringDispatcher'

Here is web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Demo1</display-name>
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>SpringDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            </param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.kurshit.springmvc.Demo1</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringDispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd"> 

 <mvc:default-servlet-handler />
 <mvc:annotation-driven/>  
<context:component-scan base-package = "com.kurshit.springmvc.Demo1.controller" />

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

</beans>

HomeController.java

package com.kurshit.springmvc.Demo1.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

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

@Controller
public class HomeController {

    @RequestMapping(value="/welcome.html")
    public ModelAndView test(HttpServletResponse response) throws IOException{
        // TODO Auto-generated method stub
        ModelAndView mav = new ModelAndView("HelloPage","msg","Welcome To Spring");


        return mav;

    }
}

HomePage.jsp

<html>
<body>
    <h1>First Spring Example with Deployment Descriptor</h1>

    <h2>${msg}</h2>

</body>
</html>

Here is how my directory structure looks like :

Directory Structure Image Link

I am using Maven tool to resolve dependancies :

Thanks!


Solution

  • Your jsp is called HomePage.jsp but you are returning HelloPage in your ModelAndView. These names need to match. (Although you don't need the .jsp extension in the ModelAndView)