首页 > 解决方案 > 如何解决 No mapping found for HTTP request with URI [/springmvc/hello] in DispatcherServlet with name 'dispatcher' 错误

问题描述

当我尝试启动 hello.jsp 页面时出现 404 错误,index.jsp 页面工作正常。但是当我尝试将地址从“ http://localhost:8080/springmvc ”更改为“ http://localhost:8080/springmvc/hello ”时,我在网页上收到“HTTP Status 404 – Not Found”错误。

我正在尝试从单独的文件夹中读取“hello.jsp”页面,即“WEB-INF/views/hello.jsp”下的视图。

我重新创建了整个项目以查找是否有任何错误。


***[MY WEB.XML CODE]***

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Hello Spring MVC</display-name>

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

***[MY DISPATCH-SERVLET.XML FILE CODE]***

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd">

    <context:component-scan base-package="com.thomas.spring.springmvc.controller" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        name="viewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
    </bean>

</beans>

***[MY CONTROLLER CLASS CODE]***

package com.thomas.spring.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public ModelAndView hello()
    {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello");
        return modelAndView;

    }

}

***[MY HELLO.JSP FILE CODE]***

<%@ 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>Hello</title>
</head>
<body>
<h1>Hello from Spring MVC!</h1>
</body>
</html>

我希望输出会收到消息“来自 Spring MVC 的你好!” 来自“hello.jsp”文件。但我收到“HTTP 状态 404 - 未找到”。

控制台输出为“Sep 12, 2019 1:51:38 PM org.springframework.web.servlet.PageNotFound noHandlerFound WARNING: No mapping found for HTTP request with URI [/springmvc/hello] in DispatcherServlet with name 'dispatcher'”。

标签: spring-mvc

解决方案


推荐阅读