在DispatcherServlet中找不到带有URI [/ HelloWeb /]的HTTP请求的映射,名称为"HelloWeb"

Abh*_*rni 6 java spring jsp

我正在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>
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

我的控制器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";
    }
 }
Run Code Online (Sandbox Code Playgroud)

为hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
  <h2>${message}</h2>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

谁能告诉我代码中有什么问题?

dav*_*rld 0

您没有/HelloWorld/仅用于的映射/hello/

转到您的基本应用程序位置localhost:8080/myApp/,然后转到/hello,您的 jsp 应该会返回。那么你的 war 文件名在localhost:8080/myApp/hello哪里myApp(假设它不是 root)。