2016-09-07 171 views
2

我是IntelliJ新手,并使用2016.2版本。以前我使用Eclipse。我正在尝试建立一个简单的Maven Spring测试项目,但我无法弄清楚什么是错的。IntelliJ IDEA 16向类路径添加maven依赖关系

注:我知道异常是什么意思,我知道使用Eclipse

注2解决方案:我试图在一个干净的理念安装

按我的理解,想法将包括Maven依赖自动(纠正我,如果我错了)

编辑1:解决方案

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

我试图做

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

异常

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet 

的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> 

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> 

p om.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> 

回答

4

按我的理解,想法会自动包含Maven依赖 (纠正我,如果我错了)

是的,如果Auto Import是在提示在检查,当您打开的IntelliJ被点击。

如果不是,请点击Intellij右侧面板上的Maven Projects,然后在refreshGenerate sources and auto import后点击按钮。这会再次触发该过程。

Maven-IntelliJ

如果以上不工作,你仍然有问题,IDE,去File -> Invalidate Cache/Restart选项。这提示如下。

enter image description here

点击Invalidate and restart,这将重新索引所有的依赖到工作区。

+1

谢谢,这帮助我解决了红色下划线的其他依赖关系。 –

+0

很高兴解决了问题,请将答案标记为已接受! – VinayVeluri

+0

显然我忘了将“ war”添加到pom.xml文件中,甚至在我添加它后没有正确导入。谢谢。 –

1

转到文件 - >设置 - >建立,执行,部署 - >编译工具 - >行家 行家主目录到你的主目录。 如果它不起作用,在文件 - >设置 - >构建,执行,部署 - > buildtools - > maven->导入选项卡,自动检查导入maven项目。

+0

您提到的设置已经默认设置。我错过了什么吗? –

0

要强制重新导入,请从“视图”菜单(或默认停靠的屏幕右侧)打开Maven项目工具窗口并按最左边的图标(看起来像一个带有箭头的蓝色圆圈)它)。
这应该强制IntelliJ从头开始解析pom并导入缺失的任何依赖关系。
如果自动导入没有像预期的那样正常工作,那么通常会比重新启动并清除缓存更快。

相关问题