我正在创建一个基本的hello world Spring MVC应用程序。但它无法找到URL映射。我搜索了很多,但没有找到任何令人满意的解决方案。我在控制台上收到以下错误。
org.springframework.web.servlet.PageNotFound noHandlerFound警告: 没有找到带有URI的HTTP请求的映射 DispatcherServlet中的[/HelloWeb/WEB-INF/jsp/hello.jsp]名称 ' HelloController中'
任何人都可以帮我解决这个问题。以下是我的代码
HelloController.java package com.tutorialspoint;
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 HelloController{
@RequestMapping(value="/" , method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
的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>HelloWeb</display-name>
<servlet>
<servlet-name>HelloController</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloController</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
HelloController中-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">
<context:component-scan base-package="com.tutorialspoint" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
</web-app>
的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>See this first MVC page here</title>
</head>
<body>
<h2>${message}</h2>
<h6>Check if above sentence is visible</h6>
</body>
</html>
答案 0 :(得分:0)
有一些我对此感到困惑的事情。
首先,不应将web.xml中的servlet命名为控制器。将其命名为其他名称,但请将该名称与spring-config-xml文件稍微匹配。也许吧
<servlet>
<servlet-name>HelloDispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
然后将您的currrent HelloController-servlet.xml命名为HelloDispatcher-servlet.xml。
其次,确定HelloController-servlet.xml位于/ WebContent / WEB-INF文件夹中?
第三,我不确定您使用哪个URL来访问您的测试应用程序,但我不是100%确定您可以只映射/,可能需要执行/ *或* .html,然后尝试使用/hello.html
答案 1 :(得分:0)
现在终于找到了我对上述问题的答案。由于错误说它无法找到确实存在的URL映射,尽管它没有运行。问题的根本原因是主控制器类未在“HelloDispatcher-servlet.xml”中定义。
<bean class="com.tutorialspoint.HelloController" />
Controller接收请求并根据使用的GET或POST方法调用适当的服务方法。 service方法将根据定义的业务逻辑设置模型数据,并将视图名称返回给DispatcherServlet。
DispatcherServlet将从ViewResolver获取帮助以获取请求的已定义视图
这就是为什么必须在“HelloDispatcher-servlet.xml”中包含控制器类
感谢大家节省时间帮助我:)