简体   繁体   English

Javax.servlet依赖项不起作用

[英]Javax.servlet dependency does not work

I am creating a Gradle project in Intellij to improve my skills because I am learning. 因为正在学习,所以我正在Intellij中创建Gradle项目以提高自己的技能。 Previously, I created the same project with Maven and It is working well. 以前,我与Maven创建了相同的项目,并且运行良好。

My project is this: Project 我的项目是这样: Project

How you can see in the image " HttpServlet " is not recognised and I don't know why because I have the dependency and apparently everything is correct, here is my build.gradle with the dependency: 在图像“ HttpServlet ”中如何看到未识别,我也不知道为什么,因为我有依赖项,而且显然一切正确,这是带有依赖项的build.gradle

group 'com.aprendiendo.java'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.5

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'

    // https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
}

In the first image It can be seen how in the Gadle Project menu -> Dependences folder is javax.servlet.... but however It does not work because I can't use anything of Java Servlet API (Classes, etc...), how you can see in the image of my project which I put before. 在第一张图片中,可以看到Gadle Project菜单 -> Dependences文件夹中的内容是javax.servlet ....但是,但是它不起作用,因为我无法使用任何Java Servlet API(类等)。 ),如何在我之前放置的项目图像中看到。

I have revised everything and I dont know what is happening. 我已经修改了所有内容,但我不知道发生了什么。

I tried a similar setup on my IntelliJ version and everything actually ended up working ok. 我在IntelliJ版本上尝试了类似的设置,但实际上一切正常。 You can try NOT using the fully qualified name in the extension. 您可以尝试在扩展名中不使用完全限定名称。 Use 采用

public class Servlet extends HttpServlet 

instead of 代替

public class Servlet extends javax.servlet.http.HttpServlet

Then hover over HttpServlet with your mouse and see if IntelliJ autosuggest an import. 然后用鼠标悬停在HttpServlet上,看看IntelliJ是否自动建议导入。 If it does then you can then use Alt + Enter to complete the import. 如果是这样,则可以使用Alt + Enter完成导入。 This may juice IntelliJ to recognize the dependency. 这可能使IntelliJ难以识别依赖关系。 You can also try Build --> Make Project to see if recognizes. 您也可以尝试Build-> Make Project来识别。

You may also want to check under File --> Project Structure and select the libraries node. 您可能还需要检查在“文件->项目结构”下,然后选择“库”节点。 Look and see if the dependencies are listed: javax:servlet-api:3.1.0, junit:junit:4.11, org.hamcrest:hamcrest-core:1.3. 查看并查看是否列出了依赖项:javax:servlet-api:3.1.0,junit:junit:4.11,org.hamcrest:hamcrest-core:1.3。

I did only test this on a later version of IntelliJ 2016, but it should work. 我只是在更高版本的IntelliJ 2016上进行了测试,但它应该可以工作。 Feel free to post back comments/questions. 随时发表评论/问题。

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

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