2

Please consider me as a beginner in spring who is in learning phase. I am trying to develop a spring MVC but for some reasons i am not able to figure out the error. Please guide. Below is my code.

TutorialController.java

package com.tutorialpoint;

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

@Controller
@RequestMapping("/hello")
public class TutorialController 
{
    @RequestMapping(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://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>SpringMVC</display-name>

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

   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>/*</url-pattern>
   </servlet-mapping>
</web-app>

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>Insert title here</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>

HelloWeb-servlet.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
   <context:component-scan base-package="com.tutorialpoint" />
    <mvc:annotation-driven />
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/jsp/" />
          <property name="suffix" value=".jsp" />
       </bean>

    </beans>

Error org.springframework.web.servlet.PageNotFound noHandlerFound WARNING: No mapping found for HTTP request with URI [/SpringMVC/] in DispatcherServlet with name 'HelloWeb'

sTg
  • 4,313
  • 16
  • 68
  • 115

2 Answers2

3

Try adding <mvc:annotation-driven/> in HelloWeb-servlet.xml, and add @Controller on TutorialController.java class.

<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-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

   <mvc:annotation-driven/>

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

</beans>
vivekpansara
  • 895
  • 1
  • 6
  • 14
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
  • Did the requested changes but still the same error...i have updated my question with the changes. – sTg Apr 27 '15 at 13:17
1

Try changing the code in web.xml as:

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

Annotate your controller class with @Controller as below:

@Controller
public class TutorialController 
{
      @RequestMapping("/hello")
       public String printHello(ModelMap model) {
          model.addAttribute("message", "Hello Spring MVC Framework!");
          return "hello";
       }
}
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
  • Tried but not working.:( – sTg Apr 27 '15 at 13:03
  • Edited my answer, might be it will work now. – Arpit Aggarwal Apr 27 '15 at 13:07
  • Tried it just now mate..still hitting the same 404. – sTg Apr 27 '15 at 13:10
  • You should try hitting url: /SpringMVC/hello. As there is no direct mapping for /SpringMVC – Arpit Aggarwal Apr 27 '15 at 13:21
  • `SpringMVC` is not the application name. `HelloWeb` is the application name.. so you have to hit `http://localhost:8080/HelloWeb/hello` – vivekpansara Apr 27 '15 at 13:33
  • HelloWeb is just the name of Servlet as far as I can see from web.xml, please check what's the context of your application. For checking the context, you can see the application deployment logs from Server(e.g.Tomcat) you are using. Usually name of your application is the context through which it is deployed by server. Considering context of your application is SpringMVC, your URL should be : http:/localhost:port/SpringMVC/hello. Thanks. – Arpit Aggarwal Apr 27 '15 at 13:37