java.lang.ClassNotFoundException:jakarta.servlet.http.HttpServlet:Spring MVC 和 Eclipse

ayu*_*ush 1 spring servlets spring-mvc maven jakarta-migration

我正在使用Spring MVC 和 Eclipse IDE创建一个 Web 应用程序。 Spring 版本 - 6.0.3
要配置项目,我按照以下步骤操作 -

  1. 在pom.xml中添加了依赖项 -
 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- Spring MVC Dependency -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>6.0.3</version>
        </dependency>
  </dependencies>
Run Code Online (Sandbox Code Playgroud)
  1. 将 Tomcat 服务器运行时添加到构建路径 - 在此输入图像描述

  2. 添加了Maven 依赖项到部署程序集 - 在此输入图像描述

  3. web.xml-(在 WEB-INF 文件夹中)

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

    <!-- Configure dispatcher servlet -->
    <servlet>
        <servlet-name>dispatcherservlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherservlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- / means handle all the requests from all urls -->
</web-app>
Run Code Online (Sandbox Code Playgroud)
  1. Dispatcherservlet-servlet.xml(在 WEB-INF 文件夹中)
<?xml version="1.0" encoding="UTF-8"?>
<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 
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context     
    http://www.springframework.org/schema/context/spring-context.xsd"

    xmlns:p="http://springframework.org/schema/p">
<!-- Enable annotations -->
<context:component-scan base-package="spring-mvc-demo.src.main.java.controller"></context:component-scan>
    <!-- View Resolver bean -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        name="viewResolver">
        <!-- Inject two properties -->
        <!-- Location for pages is given to prefix -->
        <property name="prefix" value="/WEB-INF/views/" />
        <!-- ending of page is .jsp -->
        <property name="suffix" value=".jsp" />
        <!-- Example name /WEB-INF/views/hello.jsp (here the name hello will be 
            given by controller) -->
    </bean>

</beans>


Run Code Online (Sandbox Code Playgroud)
  1. 将index.jsp放在WEB-INF-中的views文件夹下
<%@ 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 home page</h1>
        <h1>Called by home controller</h1>
        <h1>fired for /</h1>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)
  1. 在 src/main/java/controller 中创建 HomeController.java 类
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/home")
public class HomeController {
    
    @GetMapping("/current")
    public String home() {
        //return the name of the page
        System.out.println("Hello this is home URL");
        return "index";
    }
}
Run Code Online (Sandbox Code Playgroud)

我创建了一个带有 url /home/current的控制器。访问此 url 时,我希望看到所需的index.jsp
问题- 当我“在服务器上运行”时,出现以下错误-

SEVERE: Allocate exception for servlet [dispatcherservlet]
java.lang.ClassNotFoundException: jakarta.servlet.http.HttpServlet
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1412)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1220)
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
Run Code Online (Sandbox Code Playgroud)

请帮助我找到我的配置中的错误以及出现此错误的原因。我看到了其他几篇文章,重新检查了我的步骤,但仍然遇到相同的错误 - java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

Bal*_*usC 5

根据您问题中的第一个屏幕截图,您正在使用 Tomcat 9。但是对于 Spring 6,您至少需要 Tomcat 10。Tomcat 10 是第一个使用jakarta.*命名空间的,而旧版本则使用javax.*命名空间。

Spring 6(和 Spring Boot 3)是第一个使用jakarta.*命名空间的版本,而旧版本则使用javax.*命名空间。

所以你有两个选择:

  1. 将 Tomcat 至少升级到 10。
  2. 或者,将 Spring 降级至最大 5。

显然,从长远来看,选项 1 是推荐的方法。

顺便说一句,Spring 6 需要 Java 17 而不是 Java 1.7,如问题的第一个屏幕截图所示。

也可以看看:


归档时间:

查看次数:

4495 次

最近记录:

2 年,6 月 前