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

前端之家收集整理的这篇文章主要介绍了类型javax.servlet.ServletContext和javax.servlet.ServletException无法解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将 Spring Security包含在我的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

@H_301_22@

解决方法

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

您当前的pom表示您正在部署到准系统servlet容器(Tomcat,Jetty等),而不是完整的Java EE应用程序服务器(WildFly,TomEE,GlassFish,Liberty等),否则您将遇到类加载通过提供JSF与webapp而不是使用容器提供的方法解决问题.

在这种情况下,添加以下依赖关系应该适用于像Tomcat 8这样的Servlet 3.1容器:

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

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

如果您碰巧实际部署到像WildFly 8这样的Java EE 7应用服务器,请改用下面的依赖关系.它涵盖整个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.

@H_301_22@ @H_301_22@

猜你在找的Java相关文章