在名为“HelloWeb”的 DispatcherServlet 中未找到带有 URI [/HelloWeb/] 的 HTTP 请求的映射 [重复]

2024-03-04

我正在 tomcat 上部署我的项目,然后收到此错误“在名为“HelloWeb”的 DispatcherServlet 中未找到带有 URI [/HelloWeb/] 的 HTTP 请求的映射”。

这是我的 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>

my 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";
    }
 }

你好.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
  <h2>${message}</h2>
</body>
</html>

谁能建议我代码中有什么问题吗?


我相信您正在遵循以下教程教程点 http://www.tutorialspoint.com/spring/spring_mvc_hello_world_example.htm- Spring MVC 的 HelloWorld。我遇到了同样的问题,因为 DispatcherServlet 尝试解决。

http://localhost:8080/HelloWeb/

它应该是:

http://localhost:8080/HelloWeb/hello

(将其放入您的网络浏览器中)

或者可以用另一种方式完成(按照该教程中的建议)

您应该注意,在给定的 URL 中,HelloWeb 是应用程序 name 和 hello 是我们在我们的文章中提到的虚拟子文件夹 使用 @RequestMapping("/hello") 的控制器。可以直接使用root 当使用 @RequestMapping("/") 映射你的 URL 时,在这种情况下你 可以使用短URL访问同一页面http://localhost:8080/HelloWeb/但建议有不同的 不同文件夹下的功能。

in HelloWorld控制器类,你改变这个:

@RequestMapping("/hello")

to this:

@RequestMapping("/")

结果是你可以打电话http://localhost:8080/HelloWeb/没有任何问题。

希望能帮助到你!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在名为“HelloWeb”的 DispatcherServlet 中未找到带有 URI [/HelloWeb/] 的 HTTP 请求的映射 [重复] 的相关文章

随机推荐