我正在tomcat上部署我的项目,然后我收到此错误“在DispatcherServlet中找不到带有URI [/ HelloWeb /]的HTTP请求的映射,名称为'HelloWeb'”。
这是我的web xml文件 的的web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>Spring MVC Application</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>
我的 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.tutorialspoint.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
我的控制器 HelloController.java
package com.tutorialspoint.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HelloController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
的hello.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
任何人都可以告诉我代码中有什么问题吗?
答案 0 :(得分:4)
我相信您正在遵循tutorialspoint的教程 - Spring MVC的HelloWorld。我遇到了同样的问题,因为DispatcherServlet试图解决。
http://localhost:8080/HelloWeb/
应该是:
http://localhost:8080/HelloWeb/hello
(把它放在你的网络浏览器中)
或者可以采用另一种方式(如教程中所述)
在 HelloWorldController 类中,你改变了这个:您应该注意,在给定的URL中,HelloWeb是应用程序 name和hello是我们在我们提到的虚拟子文件夹 控制器使用@RequestMapping(“/ hello”)。您可以使用直接root 在使用@RequestMapping(“/”)映射您的URL时,在本例中为您 可以使用短网址访问同一页面
http://localhost:8080/HelloWeb/
但建议有不同之处 不同文件夹下的功能。
@RequestMapping("/hello")
到此:
@RequestMapping("/")
结果是您可以毫无问题地致电http://localhost:8080/HelloWeb/
。
希望它有所帮助!
答案 1 :(得分:1)
您是否检查过[PROJECT_NAME] \ target \ classes \ com \ tutorialspoint \ controller文件夹中是否生成了HelloController.class。如果不是因为编译错误而未编译您的java代码。
请检查HelloController.java中使用的所有相应java类。
答案 2 :(得分:0)
您没有/HelloWorld/
/hello/
转到您的基本应用程序位置localhost:8080/myApp/
,然后转到/hello
,您的jsp应该返回。所以localhost:8080/myApp/hello
myApp
是你的war文件名(假设它不是root)。