java - 在名称为 'HelloWeb' 的 DispatcherServlet 中找不到具有 URI [/HelloWeb/] 的 HTTP 请求的映射

标签 java spring jsp

<分区>

我正在 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>

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

我的 Controller 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>

任何人都可以告诉我代码中有什么问题吗?

最佳答案

我相信您正在学习 tutorialspoint 的教程- Spring MVC 的 HelloWorld。我遇到了同样的问题,因为 DispatcherServlet 试图解决。

http://localhost:8080/HelloWeb/

应该是:

http://localhost:8080/HelloWeb/hello

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

或者可以用另一种方式完成(如该教程中所建议的那样)

You should note that in the given URL, HelloWeb is the application name and hello is the virtual subfolder which we have mentioned in our controller using @RequestMapping("/hello"). You can use direct root while mapping your URL using @RequestMapping("/"), in this case you can access the same page using short URL http://localhost:8080/HelloWeb/ but it is advised to have different functionalities under different folders.

HelloWorldController 类中,您更改如下:

@RequestMapping("/hello")

为此:

@RequestMapping("/")

结果是您可以毫无问题地调用 http://localhost:8080/HelloWeb/

希望对您有所帮助!

关于java - 在名称为 'HelloWeb' 的 DispatcherServlet 中找不到具有 URI [/HelloWeb/] 的 HTTP 请求的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17232357/

相关文章:

java - 如何在我的 PHP 和 Java/Play Framework 应用程序之间共享一个 Apache 实例?

java - mockito 中@InjectMocks 和@Autowired 用法的区别?

jsp - SSL 连接适用于控制台应用程序但不适用于 Glassfish

java - 生成数组类型,例如 Object[].class

java - 在java中绘制三次函数图形的最佳方法

java - 如何忽略公共(public)资源(.js .css 文件)的 Servlet 过滤器

java - 无法在 native sql 查询中获取默认架构

java - Spring Boot cron 表达式问题 - 禁用启动时运行

java - 没有网络服务器的 JSP 引擎

javascript - 在网页上显示图标的问题