IntelliJ IDEA 无法解析 import javax.servlet.*;

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25589152/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:42:42  来源:igfitidea点击:

IntelliJ IDEA cannot resolve import javax.servlet.*;

javatomcatservletsintellij-idea

提问by Justin

So I had my project working perfectly and I decided to install a new hard drive (SSD) and now I cannot get it to run correctly.

所以我让我的项目完美运行,我决定安装一个新的硬盘驱动器 (SSD),现在我无法让它正常运行。

I am using Intellij and am having all of these imports have issues.

我正在使用 Intellij,并且所有这些导入都有问题。

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

They are saying cannot resolve symbol.

他们说无法解析符号。

Also in my web.xml I have this.

同样在我的 web.xml 我有这个。

<filter>
    <filter-name>LoginFilter</filter-name>
    <filter-class>com.mkyong.LoginFilter</filter-class>
</filter>

it states

它指出

com.mkyong.LoginFilter is not assignable to javax.servlet.Filter

Im going to include my web.xml just in case someone needs it.

我将包含我的 web.xml 以防万一有人需要它。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">

<display-name>JavaServerFaces</display-name>

<resource-ref>
<description>MySQL Datasource example</description>
<res-ref-name>jdbc/mkyongdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>


<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>

<!-- Welcome page -->
<welcome-file-list>
<welcome-file>faces/default.xhtml</welcome-file>
</welcome-file-list>

<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

  <!-- Map these files with JSF -->
 <servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>/faces/*</url-pattern>
 </servlet-mapping>

 <servlet-mapping>
   <servlet-name>Faces Servlet</servlet-name>
   <url-pattern>*.jsf</url-pattern>
 </servlet-mapping>

<servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.faces</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>LoginFilter</filter-name>
    <filter-class>com.mkyong.LoginFilter</filter-class>
</filter>
<!-- Set the login filter to secure all the pages in the /secured/* path of the application  -->
<filter-mapping>
    <filter-name>LoginFilter</filter-name>
    <url-pattern>/secured/*</url-pattern>
</filter-mapping>

<!-- By default go to secured welcome page -->
<welcome-file-list>
    <welcome-file>secured/welcome.xhtml</welcome-file>
</welcome-file-list>

</web-app>

This is the error I am getting now.

这是我现在遇到的错误。

   javax.servlet.ServletException: Servlet.init() for servlet Faces Servlet threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:534)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2381)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2370)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)
root cause

java.lang.IllegalStateException: Application was not properly initialized at startup, could not    find Factory: javax.faces.context.FacesContextFactory
javax.faces.FactoryFinder$FactoryManager.getFactory(FactoryFinder.java:800)
javax.faces.FactoryFinder.getFactory(FactoryFinder.java:302)
javax.faces.webapp.FacesServlet.init(FacesServlet.java:186)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:534)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2381)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2370)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

I was also using apache tomcat 8 at first then I thought that might be the issue and I changed it to 7 then to 6. If you need anything else please let me know!

我一开始也在使用 apache tomcat 8,然后我认为这可能是问题所在,我将其更改为 7,然后更改为 6。如果您需要其他任何东西,请告诉我!

here is the POM.xml as requested.

这是请求的 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/maven-v4_0_0.xsd">

 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mkyong.common</groupId>
 <artifactId>JavaServerFaces</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>JavaServerFaces Maven Webapp</name>
 <url>http://maven.apache.org</url>

<repositories>
 <repository>
   <id>java.net.m2</id>
   <name>java.net m2 repo</name>
   <url>http://download.java.net/maven/2</url>
 </repository>
</repositories>

<dependencies>

  <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-web</artifactId>
      <version>3.2.5.RELEASE</version>
  </dependency>

<!-- MySQL database driver -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.9</version>
</dependency>

<!-- Spring framework --> 
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring</artifactId>
    <version>2.5.6</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>2.5.6</version>
</dependency>


<!-- For Servlet Container like Tomcat -->
<!-- http://download.java.net/maven/2 -->
<dependency>
  <groupId>com.sun.faces</groupId>
  <artifactId>jsf-api</artifactId>
  <version>2.1.0-b03</version>
</dependency>



<!-- EL 2.2 to support method parameter in EL -->
<dependency>
  <groupId>org.glassfish.web</groupId>
  <artifactId>el-impl</artifactId>
  <version>2.2</version>
</dependency>


<!-- http://repo1.maven.org/maven2/ -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>

  <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
  </dependency>

  <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
  </dependency>


</dependencies>


 <build>
   <finalName>JavaServerFaces</finalName>

  <plugins>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <version>2.3.1</version>
       <configuration>
           <source>1.6</source>
           <target>1.6</target>
       </configuration>
   </plugin>
</plugins>

采纳答案by Filip

The servlet-api.jar is part of your server (Tomcat in this case). You need to either state a reference to that jar in your POM and mark it in 'provided' scope or (if you are not using maven for some reason) you need to add it to your classpath (the libraries of your web module in your project).

servlet-api.jar 是您的服务器(在本例中为 Tomcat)的一部分。您需要在 POM 中声明对该 jar 的引用并将其标记在“提供”范围内,或者(如果您出于某种原因没有使用 maven)您需要将其添加到您的类路径(您的 Web 模块的库)项目)。

The chances are perhaps that your Tomcat installation forder or maven repo is different after your HD reinstall. It seems to be like an environmental issue.

重新安装 HD 后,您的 Tomcat 安装程序或 ma​​ven 存储库可能有所不同。这似乎是一个环境问题。

回答by Justin

It appears that something was messed up in importing the project. I made a new project and just copied everything over and it worked perfectly. Importing was making it where the jar files could not be found. Not sure why.... Thanks for the help everyone!

似乎在导入项目时出了点问题。我做了一个新项目,然后把所有东西都复制了一遍,效果很好。导入使其无法找到 jar 文件。不知道为什么......感谢大家的帮助!

回答by Luan Vo

I found the same issue and just fixed it today. Although i have declared javax.servlet-api in my pom.xml but still got the issue. Then I fixed it by adding javax.servlet-api:3.10.jar file into project's class path.

我发现了同样的问题,今天刚刚修复。虽然我已经在我的 pom.xml 中声明了 javax.servlet-api 但仍然有问题。然后我通过将 javax.servlet-api:3.10.jar 文件添加到项目的类路径中来修复它。

enter image description here

在此处输入图片说明

回答by Jiri Vlasimsky

As somebody mentioned it above - these libraries are part of an application server so they should not be deployed to the server as other libraries like Spring and so on. You need reference them as a provided resource.

正如上面有人提到的 - 这些库是应用程序服务器的一部分,因此它们不应该像 Spring 等其他库一样部署到服务器。您需要将它们作为提供的资源进行引用。

If you are not using Maven for your project (i.e. tutorials etc.) you can go to your Project Structure settings / Modules / your module / Dependencies .. and down under the list of dependendcies is a small plus symbol (+) where you can select "Library" and after that the popup with Application Server Libraries will show. It should be selected as provided afterwards.

如果您没有为您的项目(即教程等)使用 Maven,您可以转到您的项目结构设置/模块/您的模块/依赖项 .. 在依赖项列表下方是一个小加号 (+),您可以在其中选择“Library”,然后将显示带有 Application Server Libraries 的弹出窗口。应按事后提供的方式进行选择。

Application Server Libraries Dialog

“应用程序服务器库”对话框

回答by Yani Morales

You use into terminal mvn clean install

您使用进入终端 mvn clean install

This command download all dependency, after, you go to "Maven Project" and take "Reimport All Maven Project"

此命令下载所有依赖项,然后转到“Maven Project”并执行“Reimport All Maven Project”

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by Fabian Corpuz

In my list of modules (ctrl+shift+alt+s and navigate to modules), my tomcat's scope was set to runtime. I switched it to compile time and it worked.

在我的模块列表(ctrl+shift+alt+s 并导航到模块)中,我的 tomcat 的范围设置为运行时。我将它切换到编译时间并且它起作用了。