2016-09-07 170 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並導入缺失的任何依賴關係。
如果自動導入沒有像預期的那樣正常工作,那麼通常會比重新啓動並清除緩存更快。

相關問題