简体   繁体   English

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

[英]The type javax.servlet.ServletContext and javax.servlet.ServletException cannot be resolved

I'm trying to include Spring Security to my web project, i'm following this tutorial http://docs.spring.io/spring-security/site/docs/current/guides/html5//helloworld.html我正在尝试将 Spring Security 包含到我的 Web 项目中,我正在关注本教程http://docs.spring.io/spring-security/site/docs/current/guides/html5//helloworld.html

I've done everything in the tutorial with the given maven project and works fine.我已经使用给定的 maven 项目完成了本教程中的所有操作,并且工作正常。 But when i'm trying to include it to my project a compilation error appear.但是当我试图将它包含到我的项目中时,会出现编译错误。 Specifically when i extends from AbstractSecurityWebApplicationInitializer appear the given error特别是当我从AbstractSecurityWebApplicationInitializer扩展时出现给定的错误

有错误的代码

浮动编译错误

Multiple markers at this line此行有多个标记

  • The type javax.servlet.ServletContext cannot be resolved.无法解析 javax.servlet.ServletContext 类型。 It is indirectly referenced from required .class files它是从所需的 .class 文件间接引用的
  • The type javax.servlet.ServletException cannot be resolved.无法解析 javax.servlet.ServletException 类型。 It is indirectly referenced from required .class files它是从所需的 .class 文件间接引用的

The POM.xml 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>

Thanks for the help!谢谢您的帮助!

Using mvn clean install -U使用 mvn clean install -U

在此处输入图片说明

Just add the javax.servlet API to the compile time dependencies.只需将javax.servlet API添加到编译时依赖项即可。 You don't need to include it in the build, it's already provided by the target servlet container.您不需要将它包含在构建中,它已经由目标 servlet 容器提供。

Your current pom suggests that you're deploying to a barebones servlet container ( Tomcat , Jetty , etc) instead of a full fledged Java EE application server ( WildFly , TomEE , GlassFish , Liberty , etc), otherwise you'd have run into classloading-related trouble by providing JSF along with the webapp instead of using the container-provided one.您当前的 pom 表明您正在部署到准系统 servlet 容器( TomcatJetty等)而不是成熟的 Java EE 应用程序服务器( WildFlyTomEEGlassFishLiberty等),否则您会遇到类加载通过提供 JSF 和 webapp 而不是使用容器提供的一个相关的麻烦。

In that case, adding the below dependency should do for a Servlet 3.1 container like Tomcat 8:在这种情况下,添加以下依赖项应该适用于 Tomcat 8 等Servlet 3.1容器:

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

Or if you're actually targeting an older Servlet 3.0 container like Tomcat 7, change the <version> to 3.0.1 (note: there's no 3.0 due to a mistake on their side).或者,如果您实际上是针对较旧的Servlet 3.0容器(如 Tomcat 7),请将<version>更改为3.0.1 (注意:由于他们的错误,没有3.0 )。

If you happen to actually deploy to a Java EE 7 application server like WildFly 8, use the below dependency instead.如果您碰巧实际部署到像 WildFly 8 这样的Java EE 7应用程序服务器,请改用以下依赖项。 It covers the entire Java EE API , including javax.servlet (and javax.faces , so you'd then remove those individual JSF API/impl dependencies):它涵盖了整个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>

Also here, if you're targeting an older Java EE 6 application server like JBoss AS 7, change the <version> to 6.0 .同样在这里,如果您的目标是旧的Java EE 6应用服务器,如 JBoss AS 7,请将<version>更改为6.0

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

另一种方式,如果您在 Eclipse ide 上工作,请打开项目文件夹选择属性并单击 maven 显示视图“Active Maven Profiles(逗号分隔)”请输入“dev”..刷新问题解决后

try setting the updated parent, like this:尝试设置更新的父级,如下所示:

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

this solved for me.这为我解决了。

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

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

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