1

I am creating a basic hello world Spring MVC application. But it is not able to find the URL mapping. I have searched a lot but didn't found any satisfactory solutions.I get the following error on console.

org.springframework.web.servlet.PageNotFound noHandlerFound WARNING: No mapping found for HTTP request with URI [/HelloWeb/WEB-INF/jsp/hello.jsp] in DispatcherServlet with name 'HelloController'

Can anybody help me out fixing this. Below is my code

HelloController.java package com.tutorialspoint;

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

@Controller
public class HelloController{

   @RequestMapping(value="/" , method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");

      return "hello";
   }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
 id="WebApp_ID" version="3.1">

  <display-name>HelloWeb</display-name>   
  <servlet>
      <servlet-name>HelloController</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>HelloController</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

HelloController-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:xsi="http://www.w3.org/2001/XMLSchema-instance"
   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">

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



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

hello.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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>See this first MVC page here</title>
</head>
<body>
<h2>${message}</h2>
<h6>Check if above sentence is visible</h6>
</body>
</html>
  • post the application directory structure – Balaji Krishnan Mar 16 '16 at 10:41
  • Please look into this answer http://stackoverflow.com/a/32931296/584420 – James Jithin Mar 16 '16 at 10:47
  • package(com.tutorialspoint)>HelloController.java and the webcontent structure is WEB-INF>jsp>hello.jsp also WEB-INF>HelloDispatcher-servlet.xml,web.xml – Ashish Chawla Mar 17 '16 at 04:35
  • Can a [spring-mvc] tag caretaker please take care of that [enormous list of duplicates](http://stackoverflow.com/search?q=%22No+mapping+found+for+HTTP+request+with+URI%22+%22in+DispatcherServlet+with+name%22) which is only partially shown in "Related" section on right hand side? The very same beginner's question is being asked over and over and apparently nobody cares about curating a canonical duplicate. Or does actually nobody care about [spring-mvc] these days? – BalusC Mar 17 '16 at 09:07

2 Answers2

0

There are a few things I'm slightly confused about.

First, you should not name your servlet in the web.xml to your controller. Name it something else, but do match the name somewhat with your spring-config-xml file. So perhaps <servlet> <servlet-name>HelloDispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet>

And then name your currrent HelloController-servlet.xml to HelloDispatcher-servlet.xml.

Second, are sure the HelloController-servlet.xml is in the /WebContent/WEB-INF folder?

Third, Im not sure what URL you are using to access your test application, but I'm not 100% sure you can just map /, it might be required to do /* or *.html, and then try with /hello.html

  • Yes all the xml are under '/WebContent/WEB-INF' folder structure. My project name is "HelloWeb" so the URL is "http://localhost:8080/HelloWeb/". Still no luck – Ashish Chawla Mar 16 '16 at 11:45
0

Well now finally I have found my answer to the above problem. Since the error says it could not find the URL mapping which was indeed present, though it wasn't running. The root cause of the problem was that the main controller class was not defined in the "HelloDispatcher-servlet.xml".

<bean class="com.tutorialspoint.HelloController" />

The Controller takes the request and calls the appropriate service methods based on used GET or POST method. The service method will set model data based on defined business logic and returns view name to the DispatcherServlet.

The DispatcherServlet will take help from ViewResolver to pickup the defined view for the request

That is why it is mandatory to include the controller class in the "HelloDispatcher-servlet.xml"

Thanks everybody for sparing time to help me out :)