无法解析 javax.servlet.ServletContext 和 javax.servlet.ServletException 类型 [英] The type javax.servlet.ServletContext and javax.servlet.ServletException cannot be resolved

查看:28
本文介绍了无法解析 javax.servlet.ServletContext 和 javax.servlet.ServletException 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Spring Security 包含到我的 Web 项目中,我正在学习本教程 到编译时依赖项.您不需要将它包含在构建中,它已经由目标 servlet 容器提供.

您当前的 pom 表明您正在部署到准系统 servlet 容器(TomcatJetty 等)而不是成熟的 Java EE 应用服务器(WildFlyTomEEGlassFishLiberty 等),否则您会因提供 JSF 以及webapp 而不是使用容器提供的.

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

<groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><范围>提供</范围></依赖>

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

如果您碰巧实际部署到 Java EE 7 应用服务器(如 WildFly 8),请使用下面的依赖代替.它涵盖了整个Java EE API,包括javax.servlet(和javax.faces,因此您可以删除那些单独的 JSF API/impl 依赖项):

<groupId>javax</groupId><artifactId>javaee-api</artifactId><version>7.0</version><范围>提供</范围></依赖>

同样,如果您的目标是较旧的 Java EE 6 应用服务器,例如 JBoss AS 7,将改为6.0.

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

I've done everything in the tutorial with the given maven project and works fine. 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

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

The 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

解决方案

Just add the javax.servlet API to the compile time dependencies. You don't need to include it in the build, it's already provided by the target servlet container.

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.

In that case, adding the below dependency should do for a Servlet 3.1 container like Tomcat 8:

<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).

If you happen to actually deploy to a Java EE 7 application server like WildFly 8, use the below dependency instead. It covers the entire Java EE API, including javax.servlet (and javax.faces, so you'd then remove those individual JSF API/impl dependencies):

<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.

这篇关于无法解析 javax.servlet.ServletContext 和 javax.servlet.ServletException 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆