DispatcherServlet: No mapping found for HTTP request with URI - spring

Running my project in STS gives on a Tomcat gives me the error:
Mai 19, 2016 4:44:52 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNUNG: No mapping found for HTTP request with URI [/CloudService/] in DispatcherServlet with name 'HelloWeb'
Mai 19, 2016 4:45:35 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNUNG: No mapping found for HTTP request with URI [/CloudService/HelloWeb/hello] in DispatcherServlet with name 'HelloWeb'
Mai 19, 2016 4:48:13 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNUNG: No mapping found for HTTP request with URI [/CloudService/] in DispatcherServlet with name 'HelloWeb'
I checked out nearly every relevant article on stackoverflow already, but I just cant fix my problem.
src/main/java/HelloController.java
package com.stackoverflow;
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
#RequestMapping("/hello")
public class HelloController{
#RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
WebContent/WEB-INF/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.stackoverflow" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
WebContent/WEB-INF/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>CloudService</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>
WebContent/WEB-INF/jsp/hello.jsp
<%# page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>

Your configuration looks ok. However you are requesting for the /CloudService/ url which is not mapped to any controller handler method. Your Controller has a handler for /CloudService/hello instead. You can either include a handler for / or if both / and /hello should be handled by the same method you can do it this way
#Controller
#RequestMapping({"/hello" , "/"})
public class HelloController{
#RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
N.B Your application has one application context HelloWeb-servlet.xml therefor you don`t need a ContextLoaderListener configuration to create a parent context.However as soon as you have service beans which must be separated into a different context you need to configure it

This is one sample web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
This is dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
">
<context:component-scan base-package="controllers"/>
<mvc:annotation-driven/>
<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/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean name="shape" class="shapes.Shape">
<constructor-arg index="0" value ="12" />
<constructor-arg index="1" value ="2" />
</bean>
<import resource="spring-dao.xml"/>
</beans>
I am sure this should work for you as well.

Related

Why report:“No mapping found for HTTP request with URI “

first time build a springmvc web project,use jetty as web container,but when jetty started, but when request the url:"http://localhost:8080/hello/mvc" the
broswer report:
"Problem accessing /hello/mvc. Reason: ".And the console report:"十一月 18, 2018 10:34:16 上午 org.springframework.web.servlet.PageNotFound noHandlerFound
警告: No mapping found for HTTP request with URI [/hello/mvc] in DispatcherServlet with name 'mvc-dispatcher'
"
my web.xml:
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--param-name>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-name-->
<param-value>classpath*:spring/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
mvc-dispatcher-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- scan the package and the sub package -->
<context:annotation-config/>
<context:component-scan base-package="com.zhangbing.springmvc">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- if you use annotation you must configure following setting -->
<mvc:annotation-driven />
<!-- configure the InternalResourceViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name ="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsps/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>`enter code here`
</beans>
The hierarch of my project:
enter image description here
circled by blue line is the controller
The contoller helloMvcController:
package com.zhangbing.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
#RequestMapping("/hello")
public class helloMvcController {
#RequestMapping("/mvc")
public String helloMvc()
{
return "home";
}
i search lots question like this,but no one can solved my problem,please help.
I believe, Spring can not find your controller.The problem might be with the component scan.
Please change your component scan directive from
<context:component-scan base-package="com.zhangbing.springmvc">
to
<context:component-scan base-package="com.zhangbing.springmvc.*">
Then lets see what is the output.

Tomcat 9 : 404 error

Can any one help me with this error? I am using Tomcat 9.0.0.M20 as the server in my Spring project.
HTTP Status [404] – [Not Found]
Type Status Report
Description The origin server did not find a current representation for the Target resource or is not willing to disclose that one exists.
Apache Tomcat/9.0.0.M20
Here is my code:
home.jsp
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>maventest2</title>
</head>
<body>
<h1>MavenTest2 Spring demo</h1>
</body>
</html>
Here is the web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Here is the Dispatcher servlet (dispatcher-servlet.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/cache"
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
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:component-scan base-package="com.springhello"/>
<mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Here is the Spring controller (HelloController.java):
package com.springhello.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HelloController {
#RequestMapping("/")
public String home(){
return "home";
}
}

Spring MVC Controller RequestMapping method not recognized

I have defined 2 methods in controller class . When I comment out the second method(displayHomePage), I can access the login page, but when I un-comment the second method none of the return(login.jsp or home.jsp) works. It gives HTTP 404 resource not found error. Below is my web.xml, dispatcher.xml and controller class.
Where am I going wrong? Also, if i remove "method=RequestMethod.GET" for both methods, I can access both pages without issues. It's confusing. Please help me out.
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" version="3.0">
<display-name>demo</display-name>
<servlet>
<servlet-name>demo-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>demo-dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/demo-dispatcher-servlet.xml
</param-value>
</context-param>
</web-app>
demo-dispatcher-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<context:component-scan base-package="com.abc.demo.controller" />
<context:annotation-config />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:default-servlet-handler />
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/" />
</beans>
Controller:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class LoginController {
#RequestMapping(name="/login" , method=RequestMethod.GET)
public String displayLoginPage(){
return "login";
}
/* #RequestMapping(name="/home", method=RequestMethod.GET)
public String displayHomePage(){
return "home";
}*/
}
and just login.jsp and home.jsp pages under WEB-INF/jsp folder

Spring MVC : WARNING: No mapping found for HTTP request with URI [xxx] in DispatcherServlet with name 'yyy'

I am working on Spring MVC sample. when i use the / it works well.
but when it comes to /xxx, it return WARNING.
WARNING: No mapping found for HTTP request with URI [/SimpleSpringMVC/index] in DispatcherServlet with name 'report'
I had searched many answers, but all of them don't work well.
And here are details of my sample.
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:tool.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>report</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/report-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>report</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
report-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:utils="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<mvc:annotation-driven />
<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/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
SimpleController.java
package com.tian.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller("Report")
public class SimpleController {
public SimpleController() {
System.out.println("Initial ...");
}
#RequestMapping("/index")
public String home() {
return "home";
}
#RequestMapping(value = "/info", method = RequestMethod.GET)
public #ResponseBody String getInfo() {
return "hello world";
}
}
tool.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
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.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.tian.controller" />
</beans>
Ok, i don' t know what is tool.xml for, but i would do something like this, put this line in report-servlet.xml <context:component-scan base-package="com.tian.controller" /> before <mvc:annotation-driven /> then in your web.xml put something like this :
<servlet>
<servlet-name>report</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>report</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
And take a look at this controller,
#Controller
#RequestMapping(value="/login")
public class LoginController {
#RequestMapping(method=RequestMethod.POST)
#ResponseBody
public String receiveLoginRequest(#RequestParam String username,#RequestParam String password){
//DO WHAT YOU WANT
}
}
And when you test a controller, localhost:8080/report/login/YOURMETHODMAP
You want to understand how to config your context my friend so instead of giving you a botched answer that doesn't really help you, here is a beautiful answer : ANSWER BY informatik01, giving you a nice and simple lecture
Long story short web application contexts are hierarchical and your mapping is ignored (more or less)
Hope this helps.

Spring mvc resources not works

I created ,with Eclipse Luna, a simple Spring MVC Project but it doesn't works if i add mvc:resources in springConfig-servlet.xml. I have this errors in Tomcat:
PM org.springframework.web.servlet.PageNotFound noHandlerFound
Avvertenza: No mapping found for HTTP request with URI [/SpringStore/] in DispatcherServlet with name 'springConfig'
Where am I doing wrong?Thanks
This is the structure of my project
Web.xml code
<?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>SpringStore</display-name>
<servlet>
<servlet-name>springConfig</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springConfig</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springConfig-Servlet code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<context:component-scan base-package="controller"/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
</beans>
HomeController.java
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class HomeController {
#RequestMapping(value="/",method=RequestMethod.GET)
public String welcome(){
return "index";
}//welcome
}
The contents of the file index.jsp is very long and I don't write it here.
I think you are missing below entries in your springConfig-Servlet.xml file.
<mvc:default-servlet-handler />
<mvc:annotation-driven />

Resources