[英]No mapping found for HTTP request with URI… in DispatcherServlet with name
[英]No mapping found for HTTP request with URI xxx in DispatcherServlet with name xx
我查看了幾乎所有關於該錯誤的主題,但沒有一個對我有用。
我創建了一個簡單的Spring MVC項目,當我運行該項目時,我得到了這個錯誤:
févr. 19, 2016 12:29:56 PM org.springframework.web.servlet.PageNotFound noHandlerFound
AVERTISSEMENT: No mapping found for HTTP request with URI [/SpringMVCsample1/] in DispatcherServlet with name 'one'
這是我的代碼:
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMVCsample1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>jsp/hello.jsp</welcome-file>
</welcome-file-list>
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>one</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>one</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
one-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">
<!-- This will allow Spring to load all the components from package com.capgemini.springtest -->
<!-- and all its child packages. -->
<context:component-scan base-package="com.capgemini.springtest" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
OneController.java:
package com.capgemini.springtest;
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 OneController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
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>Insert title here</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
有任何想法嗎 ?
在web xml中更改url-pattern:
<url-pattern>/*</url-pattern>
更改控制器以具有根上下文的映射:
package com.capgemini.springtest;
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 OneController {
@RequestMapping(value = {"/","/hello"}, method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
hello.jsp應該位於文件夾:/WEB-INF/jsp/hello.jsp下
當然你會遇到這樣的問題,因為你沒有任何控制器來處理請求“/”你要做的就是添加一個控制器或在你的web文件夾下添加index.jsp
web
|--->WEB-INF
| |--->jsp
| |---->index.jsp
|--->index.jsp
在web.xml
您的servlet僅映射/
URI。 URL模式應該是/*
。
嘗試將<mvc:annotation-driven />
到one-servlet.xml中 。 記得在那里添加mvc
名稱空間的定義xmlns:mvc="http://www.springframework.org/schema/mvc"
然后嘗試訪問localhost:8080/SpringMVCsample1/hello
。 不是localhost:8080/SpringMVCsample1/hello/
就像你說過你在其中一條評論中嘗試過的那樣
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.