IntelliJ IDEA 16将maven依赖项添加到classpath

Duy*_*Anh 5 java spring intellij-idea maven

我是IntelliJ的新手并使用2016.2版本.以前我在使用Eclipse.我正在尝试建立一个简单的maven spring测试项目,但是我无法弄清楚出了什么问题.

注意:我知道异常意味着什么,并且我知道使用Eclipse的解决方案

注2:我尝试了一个干净的Idea安装

根据我的理解,想法将自动包括maven依赖(如果我错了,请纠正我)

编辑1: 解决方案

  1. 项目 - >右键单击 - >添加框架支持 - >检查Spring/Spring MVC
  2. <packaging>war</packaging>
  3. 重新导入maven依赖项

我试图做什么

  1. 重新导入maven依赖项
  2. 关闭IntelliJ并删除所有*.iml文件和所有.idea文件夹

例外

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
Run Code Online (Sandbox Code Playgroud)

web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <servlet-name>sample2</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>sample2</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
Run Code Online (Sandbox Code Playgroud)

SAMPLE2-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.test"></context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>
Run Code Online (Sandbox Code Playgroud)

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.sa</groupId>
    <artifactId>sample2</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</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-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

</project>
Run Code Online (Sandbox Code Playgroud)

Vin*_*uri 11

根据我的理解,想法将自动包括maven依赖(如果我错了,请纠正我)

是的,如果Auto Import在提示时检入,则在您打开IntelliJ时单击.

如果没有,请点击Maven ProjectsIntellij的右侧窗格,然后在refresh- > 后单击按钮Generate sources and auto import.这会再次触发该过程.

Maven的的IntelliJ

如果以上操作无效并且您仍然遇到IDE问题,请转到File -> Invalidate Cache/Restart选项.提示如下.

在此输入图像描述

单击Invalidate and restart,这将重新索引所有依赖项到工作区.