无法解析 javax.servlet.ServletContext 和 javax.servlet.ServletException 类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30737617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The type javax.servlet.ServletContext and javax.servlet.ServletException cannot be resolved
提问by Amaury Esparza
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. 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
我已经使用给定的 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
此行有多个标记
- 无法解析 javax.servlet.ServletContext 类型。它是从所需的 .class 文件间接引用的
- 无法解析 javax.servlet.ServletException 类型。它是从所需的 .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
采纳答案by BalusC
Just add the javax.servlet
APIto the compile time dependencies. You don't need to include it in the build, it's already provided by the target servlet container.
只需将javax.servlet
API添加到编译时依赖项即可。您不需要将它包含在构建中,它已经由目标 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 容器(Tomcat、Jetty等)而不是成熟的 Java EE 应用程序服务器(WildFly、TomEE、GlassFish、Liberty等),否则您会遇到类加载通过提供 JSF 和 webapp 而不是使用容器提供的一个相关的麻烦。
In that case, adding the below dependency should do for a Servlet 3.1container 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.0container 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 7application 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):
如果您碰巧实际部署到像 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>
Also here, if you're targeting an older Java EE 6application server like JBoss AS 7, change the <version>
to 6.0
.
另外这里,如果你的目标较旧的Java EE 6应用服务器像JBoss AS 7,改<version>
到6.0
。
回答by Dila Gurung
This worked for me: if above supplied solution doesnt work Project > Properties > Java Build Path > Libraries > Add library from library tab > Choose server runtime > Next > choose Apache Tomcat v 7.0> Finish > Ok
这对我有用:如果上面提供的解决方案不起作用项目>属性>Java构建路径>库>从库选项卡添加库>选择服务器运行时>下一步>选择Apache Tomcat v 7.0>完成>确定
回答by Digao
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.
这为我解决了。
回答by AlieneilA
What helped for me was deleting the repository in directory .m2 and after deleting run maven: package
对我有帮助的是删除目录 .m2 中的存储库并删除 run maven: package
回答by Prince Aveiroz
another way if you working on eclipse ide please open the project folder select Properties and click maven shows the view 'Active Maven Profiles(comma separated)'please input "dev"..after refresh problem solved
另一种方式,如果您在 Eclipse ide 上工作,请打开项目文件夹选择属性并单击 maven 显示视图“Active Maven Profiles(逗号分隔)”,请输入“dev”..刷新问题解决后