![](/img/trans.png)
[英]org.springframework.web.servlet.DispatcherServlet noHandlerFound
[英]org.springframework.web.servlet.DispatcherServlet noHandlerFound : Spring MVC
我是春季MVC的新手。 我嘗試創建小的Hello World應用程序,但未按預期運行。 我總是收到錯誤org.springframework.web.servlet.DispatcherServlet noHandlerFound警告:在DispatcherServlet中,名稱為“ fitTrackerServlet”的URI [/FitnessTracker/greeting.html]的HTTP請求找不到映射。
我知道這個錯誤非常普遍,Google上有很多可用的鏈接,但沒有一個對我有用。 任何幫助,將不勝感激。
這是代碼片段
HelloController.java
@Controller
public class HelloController {
@RequestMapping(value = "/greeting")
public String sayHello(Model model) {
model.addAttribute("greeting","Hello World!!!!");
return "hello";
}}
hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>
Insert title here
</title>
</head>
<body>
<h1>
${greeting}
</h1>
</body>
</html>
fitTrackerServlet-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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
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">
<mvc:annotation-driven/>
<context:annotation-config/>
<context:component-scan base-package="com.firstspringmvc.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp"/>
</beans>
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"
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>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>fitTrackerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>fitTrackerServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
URL /greeting
末尾沒有任何.html
,因此Servlet將不會使用DispatcherServlet,因為*.html
模式與/ greeting不匹配,這將導致404。
更改您的web.xml,例如:
<servlet-mapping>
<servlet-name>fitTrackerServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.