Prashant Singh
Prashant Singh

Reputation: 123

Getting this org.springframework.web.servlet.DispatcherServlet noHandlerFound error and WARNING: No mapping found for HTTP request URI in Spring MVC

enter image description here Index.jsp

<html>
    <body>
        <form action="add">
            <input type="text" name="t1"><br>
            <input type="text" name="t2"><br>
            <input type="submit" />
        </form>
    </body>
</html>

Web.xml

<!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>telusko</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>telusko</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

telusko-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:ctx="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-2.5.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
    <!-- <mvc:default-servlet-handler/>  -->
    <mvc:annotation-driven/>
    <ctx:annotation-config></ctx:annotation-config>
    <ctx:component-scan base-package="resources.com.telusko"></ctx:component-scan>
</beans>

AddController.java

package com.telusko;

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

@Controller
public class AddController {
    @RequestMapping("/add")
    public void add() {
        System.out.println("i am here");
    }
}

I have an index.jsp file where I declare action=/add. Now in web.xml in declare a dispatcher in which given servlet name login. After that make a telusko-servlet.xml where define base-package:

<ctx:component-scan base-package="com.telusko"></ctx:component-scan>

After that make a class of logincontroller and use annotaion @Controller above class and @RequestMapping("/add) above method of logincontroller class. And after that running the index.jsp file still giving me error why noHandlerFound error occurs as in login-servlet.xml. I have given base-package name com.telusko where AddController.java file is there.

I should get "i am here" message on console but not getting it instead of that getting WARNING: No mapping found for HTTP request

Sep 26, 2018 10:47:36 PM org.springframework.web.servlet.DispatcherServlet 
initServletBean
INFO: FrameworkServlet 'telusko': initialization started
Sep 26, 2018 10:47:36 PM 
org.springframework.web.context.support.XmlWebApplicationContext 
prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'telusko-servlet': 
startup date [Wed Sep 26 22:47:36 IST 2018]; root of context hierarchy
Sep 26, 2018 10:47:36 PM 
org.springframework.beans.factory.xml.XmlBeanDefinitionReader 
loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB- 
INF/telusko-servlet.xml]
Sep 26, 2018 10:47:38 PM org.springframework.web.servlet.DispatcherServlet 
initServletBean
INFO: FrameworkServlet 'telusko': initialization completed in 2096 ms
Sep 26, 2018 10:47:38 PM org.apache.catalina.core.StandardWrapperValve 
invoke
SEVERE: Servlet.service() for servlet [telusko] in context with path 
[/DemoMVC] threw exception [Servlet execution threw an exception] with root 
cause
java.lang.NoSuchMethodError:   
org.springframework.http.HttpMethod.resolve(Ljava/lang/String;)
Lorg/springframe 
ork/http/HttpMethod;
at 

org.springframework.web.servlet.FrameworkServlet.service
(FrameworkServlet.java:8 
41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at 

org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
 (ApplicationFilterChain.java:231)
 at 


 org.apache.catalina.core.ApplicationFilterChain.doFilter
 (ApplicationFilterChain.java:166)
  at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
 at 

 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:193)
at 

org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:166 at 
org.apache.catalina.core.StandardWrapperValve.invoke
(StandardWrapperValve.java:199)at 

org.apache.catalina.core.StandardContextValve.invoke
 (StandardContextValve.java:96)at 

org.apache.catalina.authenticator.AuthenticatorBase.invoke
(AuthenticatorBase.jav 
a:490)
at 
org.apache.catalina.core.StandardHostValve.invoke
(StandardHostValve.java:139)
    at 
org.apache.catalina.valves.ErrorReportValve.invoke
(ErrorReportValve.java:92)
at 

org.apache.catalina.valves.AbstractAccessLogValve.invoke
(AbstractAccessLogValve. 
java:668)
at 

org.apache.catalina.core.StandardEngineValve.invoke
(StandardEngineValve.java:74)
at 
org.apache.catalina.connector.CoyoteAdapter.service
(CoyoteAdapter.java:343)
at 
org.apache.coyote.http11.Http11Processor.service
(Http11Processor.java:408)at 

org.apache.coyote.AbstractProcessorLight.process
 (AbstractProcessorLight.java:66)at 

org.apache.coyote.AbstractProtocol$ConnectionHandler.process
(AbstractProtocol.ja 
va:770)at 

org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun
(NioEndpoint.java:1415)at 

org.apache.tomcat.util.net.SocketProcessorBase.run
(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at 



org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run
(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

Upvotes: 4

Views: 18473

Answers (10)

Debjyoti Roy
Debjyoti Roy

Reputation: 1

Putting "views" folder under "Deployment Assembly" did the trick for me!

Upvotes: 0

Praveen Kumar Singh
Praveen Kumar Singh

Reputation: 21

Dear guys after googling so many hours I finally got the answer for Solution to Error 404:

Error: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

If you working in STS and in STS dispatcherServlet noHandler found error will be show......So to clear this step first you go on this step : Right click on the project > Build Path > Configure Build path > Deployment Assembly > Add(right hand side) > Folder > Add your views folder > Apply and Close

Then after you go through first step then your index page will be shown but your controller url show error.....show for this procedure u have to build the project is sts because in eclipse the project will be automatically build....so in sts we have to do manually....and for this the steps is : Right click on the project > Go to Build Project ...... then after building project will be done then go to project>run on server and then project will run...and index and controller both url will be working fine...

Hope this will be helpful 🙂

Upvotes: 0

Ratnesh K
Ratnesh K

Reputation: 29

I was facing the same issue in my code when i missed the forward slash(/) on handler method :

@RequestMapping(path="processform", method=RequestMethod.POST)
public String handleForm(@RequestParam("userName") String userName,
        @RequestParam("email") String userEmail,
        @RequestParam("password") String userPassword, Model model)
{
   
    System.out.println("user Name: "+ userName);
    System.out.println("user email: "+ userEmail);
    System.out.println("user Password: "+ userPassword);    
    model.addAttribute("name", userName);
    model.addAttribute("email", userEmail );
    model.addAttribute("password", userPassword);
    
    return"success";
}

Solution :Add forward slash(/ )properly on handler method and add view resolver properly in servlet configuration file.

@RequestMapping(path="/processform", method=RequestMethod.POST)

view resolver

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

Upvotes: 0

naughty7
naughty7

Reputation: 11

Create a new folder /src/main/java inside the project and add addcontroller.java file inside it. This works for me.

enter image description here

Upvotes: 1

AmeeQ
AmeeQ

Reputation: 127

I was facing the same issue none of the StackOverflow answers were working. After making some changes in the web.xml it resolved my issue. I just add the path of my dispatcher-servlet.xml in the web.xml. Image attached.

enter image description here

Upvotes: 0

Jyot
Jyot

Reputation: 540

After spending whole day i got to know the solution. When you create maven project, by default it doesn't create 'java' folder under 'javaResource/src/main', which is required to get the controller path.

Make sure you create java folder first inside 'javaResource/src/main', then create your package and controller.

Screenshot attached for reference.

enter image description here

Upvotes: 0

SumanthKethe
SumanthKethe

Reputation: 91

I have faced the same problem and below change in project folder structure solved my issue.

Change the folder structure as below, if there is no java folder available , create it. your AddController.java should be present under /src/main/java. After this change you can see the SOP in the logs.

/src/main/resources to /src/main/java

Upvotes: 2

Raja M
Raja M

Reputation: 21

@Component
@RequestMapping(path="/add")
public class AddController {

    @RequestMapping(method=RequestMethod.GET)
    public void add() {
        System.out.println("I'm here");
    }
}

Upvotes: 2

Theblank
Theblank

Reputation: 11

In the project structure, the com.telusko package where the controller is created should lie under the Java Resources/src/main/java folder which is missing and can be created by going through the project properties and clicking the Java Build Path. In there, please choose the source tab and remove the missing folder. and again create or add the folder with same folder path and name. This will create a folder in project structure as src/main/java. Inside this path, com.telusko package with controller should be moved or created. And again clean and run the server. It will work now.

Upvotes: 1

Huy Nguyen
Huy Nguyen

Reputation: 2061

Add viewResolver bean in your configuration, i suppose your view in "WEB-INF/jsp"

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

Edit :

<ctx:component-scan base-package="resources.com.telusko"></ctx:component- 
scan>

to

<ctx:component-scan base-package="com.telusko"></ctx:component- 
scan>

Change your request method

@RequestMapping("/add")
public String add()
{
    System.out.println("i am here");
    return "index"; //return your view to render
}

Hope it work.

Upvotes: 0

Related Questions