0

I had some problems when I first wrote the Spring MVC project enter image description here My pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>springmvc01-base</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>springmvc01-base Maven Webapp</name>
  <url>http://maven.apache.org</url>

    <dependencies>
        <!-- SpringMVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.1</version>
        </dependency>

        <!-- 日志 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
            <scope>provided</scope>
        </dependency>


        <!-- Spring5和Thymeleaf整合包 -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.0.12.RELEASE</version>
        </dependency>
    </dependencies>
  <build>
    <finalName>springmvc01-base</finalName>
  </build>

</project>

I've added servlets in my dependencies and built successfully with Maven enter image description here

However, servlet-api does not appear in the target/project/WEB-INF/lib directory enter image description here

Can you help me?

I tried clear and package maven and it didn't work

vaibhavsahu
  • 612
  • 2
  • 7
  • 19
  • With scope=provided, it's normal that the servlet jar doesn't appear in your WEB-INF/lib. It's expected to be part of your server environment - Tomcat - rather than your app. – dbreaux Aug 07 '23 at 22:01
  • I'd normally say this looks like a case of mismatched dependency and Tomcat versions (see, for instance, https://stackoverflow.com/q/54122724/796761). But It looks like Tomcat 9 is what you're running, which does support Servlet 4 that you've specified, which does have the method specified. – dbreaux Aug 07 '23 at 22:07

1 Answers1

-2

Please try changing

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

to

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

see Maven Dependency Scope

If you want to deploy with a Servlet container, and the container already provides an appropriate version of the Servlet API, you can use <scope>provided</scope>; if you don't want to rely on an external Servlet container, you'd better delete <scope> provided</scope>.

This is my understanding, I hope it can help you.

HuaJFrame
  • 72
  • 7