简体   繁体   English

Spring MVC - 出现 404 错误并且无法配置 Dispatcher Servlet

[英]Spring MVC - Getting 404 error and not able to configure Dispatcher Servlet

I am new to Spring MVC and trying to create my first project in Spring MVC.我是 Spring MVC 的新手,并试图在 Spring MVC 中创建我的第一个项目。 Please help me to resolve the following error: -请帮我解决以下错误:-

Getting following error in console: -在控制台中出现以下错误:-

Allocate exception for servlet [dispatcher]
java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet

Browser showing 404 error for the URL - http://localhost:9492/MvcProject/home浏览器显示 URL - http://localhost:9492/MvcProject/home 的 404 错误在此处输入图像描述

web.xml web.xml

<!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>Archetype Created Web Application</display-name>
  
  <!-- Configure Dispatcher Servlet -->
  <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>    <!-- handles all requests starting with '/' -->
  </servlet-mapping>
</web-app>

dispatcher-servlet.xml调度程序-servlet.xml

<?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"
    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">
    
    <context:component-scan base-package="mvc.controller"/>
    <context:annotation-config/>

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

Controller Class Controller Class

package mvc.controller;

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

@Controller
public class HomeController {
    
    @RequestMapping("/home")
    public String home() {
        System.out.println("This is home page");
        return "index1";
    }
}

index1.jsp索引1.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home Page</title>
</head>
<body>
    <h1>This is our home page - index1</h1>
</body>
</html>

Eclipse Project hierarchy Eclipse 项目层次结构

在此处输入图像描述

You have to add javax.servlet-api dependency in the pom.xml file.您必须在 pom.xml 文件中添加 javax.servlet-api 依赖项。 After adding, apply Maven update and it will download the jar file.添加后,应用 Maven 更新,它将下载 jar 文件。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>

Try to add javax.servlet-api.jar to your WEB-INF/lib directory.尝试将 javax.servlet-api.jar 添加到您的 WEB-INF/lib 目录。 Maybe it is not in tomcat's lib folder.也许它不在tomcat的lib文件夹中。 To download the jar go to下载 jar go 到

https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api/3.1.0 https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api/3.1.0

Allocate exception for servlet [dispatcher] java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet为 servlet [dispatcher] java.lang.ClassNotFoundException 分配异常:javax.servlet.http.HttpServlet

Means your application is trying to use the class javax.servlet.http.HttpServlet but it can't find in your POJO.意味着您的应用程序正在尝试使用 class javax.servlet.http.HttpServlet但在您的 POJO 中找不到。 You have to add HttpServlet dependency in your project.您必须在项目中添加HttpServlet依赖项。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
</dependency>

Thanks I tried to add javax serlvet dependency, but it didn't work with Tomcat 10 as it switched from javax to jakarta package.谢谢我尝试添加 javax serlvet 依赖项,但它不适用于 Tomcat 10,因为它从 javax 切换到 jakarta package。 Then I further researched and got to know Spring MVC doesn't have compatibility with Tomcat 10 as it stills depends on javax.然后我进一步研究并了解 Spring MVC 与 Tomcat 10 不兼容,因为它仍然依赖于 javax。 Please read the following for more detail: - Deploying Spring 5.x on Tomcat 10.x请阅读以下内容以获取更多详细信息: - 在 Tomcat 10.x 上部署 Spring 5.x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM