Java 即使添加依赖项后,import org.springframework 也无法解析

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

import org.springframework cannot be resolve even after adding dependencies

javaspringmavenspring-mvc

提问by sharon Hwk

I get errors that say The import org.springframework cannot be resolvedfor all the imports. My POM.XML is as follows: i am sure that i have included all the necessary dependencies. No clue, why this is happening. Help

我收到The import org.springframework cannot be resolved所有进口商品的错误信息。我的 POM.XML 如下:我确信我已经包含了所有必要的依赖项。不知道为什么会这样。帮助

POM.XML

POM文件

<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>com.app.controller</groupId>
    <artifactId>FirstController</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>webapp spring</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring.version>4.0.0.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- Spring 3 dependencies -->


            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>FirstController</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

FirstController.java

第一控制器.java

package my.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/hi")
public class FirstController {
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Spring 3 MVC Hello LA");
        return "hello";
    }
}

回答by mtk

  • Check the output of mvn clean install. And see if the concerned jar is being downloaded. Look into $HOME/.m2/repo/....to see if it is indeed present.

  • In the IDE be sure you have configured the Build Path correctly (e.g. in eclipse, Right-click project, Build-Path, 3rd tab)

  • Besides, I guess it should be pom.xml(lowercase) and not POM.XML, as I believe case matters. But I guess you put that in the post unkownigly.

  • 检查输出mvn clean install。并查看是否正在下载相关的 jar。查看$HOME/.m2/repo/....它是否确实存在。

  • 在 IDE 中,确保您已正确配置构建路径(例如,在 Eclipse 中,右键单击项目,构建路径,第 3 个选项卡)

  • 此外,我想它应该是pom.xml(小写)而不是POM.XML,因为我认为大小写很重要。但我猜你是不知情地把它放在了帖子里。

回答by feng smith

I had met this problem just a moment ago, I just ran a spring mvc demo.My IDE is IntellJ, I just reimported all maven projects, everything is OK now. I just clicked the right top corner reimport all maven project button, everything is OK, you can have a try.

刚刚遇到这个问题,刚跑了一个spring mvc demo。我的IDE是IntellJ,我刚刚重新导入了所有maven项目,现在一切正常。我刚点了右上角的reimport all maven project按钮,一切正常,你可以试试。

回答by Marcel Bussien

I solved the problem by right click on the pom.xml in Eclipse and 'Maven Add Dependency' org.springframework spring-web 3.0.4.RELEASE

我通过右键单击 Eclipse 中的 pom.xml 和“Maven Add Dependency”解决了这个问题 org.springframework spring-web 3.0.4.RELEASE

回答by Weidong Fang

My problem was that the M2_REPO variable was not configured properly.

我的问题是 M2_REPO 变量没有正确配置。

Solved by go to project Properties => Java Build Path => Add Variable => Configure Variables => New and added M2_REPO to be ~/.m2/repository.

通过转到项目属性 => Java 构建路径 => 添加变量 => 配置变量 => 新建并将 M2_REPO 添加到 ~/.m2/repository 来解决。

回答by Alina_Lapina

I ItelliJ: Maven Projects -> Reimport

I ItelliJ:Maven 项目 -> 重新导入

enter image description here

在此处输入图片说明

回答by Siddharth

I resolved it by deleting all the Problems (select all the Problems, delete it) and doing a Project Clean. This works for the the most stubborn of issues. For some reason Eclipse does not delete all the Problems before doing a clean.

我通过删除所有问题(选择所有问题,删除它)并进行项目清理来解决它。这适用于最顽固的问题。出于某种原因,Eclipse 在进行清理之前不会删除所有问题。

回答by Tan Dao

it's a very common issue with people who new to Eclipse with Maven. To fix just follow steps below:

对于刚接触 Maven 的 Eclipse 的人来说,这是一个非常常见的问题。要修复只需按照以下步骤操作:

  1. Select the project in Eclipse
  2. Right clich to open context menu.
  3. Select Maven/Update Project( or Alt + F5)
  1. 在 Eclipse 中选择项目
  2. 右键打开上下文菜单
  3. 选择Maven/更新项目(或 Alt + F5)

Elipse then will reload/update new dependencies for you.

然后 Elipse 将为您重新加载/更新新的依赖项。

Happy coding!

快乐编码!

回答by Kunle Ajiboye

Just paste this in your pom.xml

只需将其粘贴到您的 pom.xml 中

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

回答by JavaSheriff

you are using annotations from later versions of Spring
Update your Spring version in the pom.xml to reflect a more current correct Spring version

您正在使用来自更高版本 Spring 的注释
在 pom.xml 中更新您的 Spring 版本以反映更当前正确的 Spring 版本

From

 <spring.version>4.0.0.RELEASE</spring.version>

To

 <spring.version>4.3.0.RELEASE</spring.version>

回答by Subodhi93

Simply change spring version from

只需将 spring 版本从

<spring.version>4.0.0.RELEASE</spring.version>

to

<spring.version>3.0.0.RELEASE</spring.version>