6

I am deploying my project on tomcat,then I am getting this error "No mapping found for HTTP request with URI [/HelloWeb/] in DispatcherServlet with name 'HelloWeb'".

this is my web xml file web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 version="3.0"
 metadata-complete="true">

<display-name>Spring MVC Application</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>

my HelloWeb-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-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.controller" />

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

</beans>

my Controller HelloController.java

package com.tutorialspoint.controller;

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
public class HelloController {

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

       return "hello";
    }
 }

hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
  <h2>${message}</h2>
</body>
</html>

Can anyone please suggest me what is wrong in code?

0

3 Answers 3

4

I believe that you are following the tutorial of tutorialspoint - HelloWorld for Spring MVC. I have the same issue which happens because DispatcherServlet try to resolve.

http://localhost:8080/HelloWeb/

It should be:

http://localhost:8080/HelloWeb/hello

(Put this in your web-browser)

Or it can be done another way (as suggested in that tutorial)

You should note that in the given URL, HelloWeb is the application name and hello is the virtual subfolder which we have mentioned in our controller using @RequestMapping("/hello"). You can use direct root while mapping your URL using @RequestMapping("/"), in this case you can access the same page using short URL http://localhost:8080/HelloWeb/ but it is advised to have different functionalities under different folders.

in HelloWorldController class, you change this:

@RequestMapping("/hello")

to this:

@RequestMapping("/")

the result is that you can call http://localhost:8080/HelloWeb/ without any problem.

Hope it helps!

1

Did you check that HelloController.class is generated or not in your [PROJECT_NAME]\target\classes\com\tutorialspoint\controller folder. If not your java code is not compiled due to some compilation errors.

Please check all corresponding java classes used in HelloController.java.

0

You don't have a mapping for /HelloWorld/ only /hello/

Go to your base application location localhost:8080/myApp/ then go to /hello and your jsp should return. So localhost:8080/myApp/hello where myApp is your war filename (assuming it's not root).

1
  • 1
    Thanks for replay...Issue is resolved because i forgot to write the code in web.xml <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> Jun 21, 2013 at 13:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.