无法解析类型 javax.servlet.ServletContext 和 javax.servlet.ServletException

The type javax.servlet.ServletContext and javax.servlet.ServletException cannot be resolved

我正在尝试将 Spring 安全性包括到我的 Web 项目中,我正在学习本教程 http://docs.spring.io/spring-security/site/docs/current/guides/html5//helloworld.html

我已经使用给定的 maven 项目完成了教程中的所有操作,并且工作正常。但是当我试图将它包含到我的项目中时出现编译错误。具体来说,当我从 AbstractSecurityWebApplicationInitializer 扩展时出现给定的错误

Multiple markers at this line

  • The type javax.servlet.ServletContext cannot be resolved. It is indirectly referenced from required .class files
  • The type javax.servlet.ServletException cannot be resolved. It is indirectly referenced from required .class files

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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring.primefaces</groupId>
<artifactId>primefaces.testwebapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringPrimefacesWebApp</name>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <!-- JSF 2.2 core and implementation -->
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.11</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.11</version>
    </dependency>
    <!-- Prime Faces -->
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>5.2</version>
    </dependency>
    <!-- Spring security -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>4.0.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>4.0.1.RELEASE</version>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.1.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

感谢您的帮助!

使用 mvn clean install -U

只需将 javax.servlet API 添加到编译时依赖项中。您不需要将它包含在构建中,它已经由目标 servlet 容器提供。

您当前的 pom 建议您部署到准系统 servlet 容器(Tomcat, Jetty, etc) instead of a full fledged Java EE application server (WildFly, TomEE, GlassFish, Liberty,等等),否则您会 运行 通过提供 JSF 和webapp 而不是使用容器提供的。

在这种情况下,添加以下依赖项应该适用于 Servlet 3.1 容器,例如 Tomcat 8:

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

或者,如果您实际上是针对 Tomcat 7 等较旧的 Servlet 3.0 容器,请将 <version> 更改为 3.0.1(注意:没有 3.0 由于他们这边的失误)。

如果您碰巧实际部署到 Java EE 7 application server like WildFly 8, use the below dependency instead. It covers the entire Java EE API,包括 javax.servlet(和 javax.faces,那么您将删除那些单独的 JSF API/impl 依赖项):

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>

此外,如果您的目标是较旧的 Java EE 6 应用程序服务器,例如 JBoss AS 7,请将 <version> 更改为 6.0

这对我有用:如果上面提供的解决方案不起作用 项目 > 属性 > Java 构建路径 > 库 > 从库选项卡添加库 > 选择服务器运行时 > 下一步 > 选择 Apache Tomcat v 7.0> 完成 > 确定

尝试设置更新后的父级,如下所示:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.6.RELEASE</version>
    <relativePath></relativePath>
</parent>

这为我解决了。

对我有帮助的是删除目录 .m2 中的存储库 在删除 运行 maven: package

之后

另一种方式,如果你使用 eclipse ide 请打开项目文件夹 select 属性并单击 maven 显示视图“Active Maven Profiles(逗号分隔)”请输入 "dev" ..刷新问题解决后