1、maven項目在install時,報錯

  • 報錯信息:

-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable

  • 解決方案

1.添加M2_HOME的環境變量
2.Preference->Java->Installed JREs->Edit 選擇一個jdk,
添加 -Dmaven.multiModuleProjectDirectory=$M2_HOME


-Dmaven.multiModuleProjectDirectory=$M2_HOME


2、eclipse啟動慢卡

  • 關閉不必要的啟動項:window——Preferences——General——Startup and Shutdown 中,把Plug-ins activated on startup下面不用的都不要勾選。

  • eclipse.ini配置文件的修改(這個應該懂吧)。

3、用maven創建不了項目

  • 首先查看maven是否成功安裝以及配置

  • 將生成的.m2文件刪掉,再創建項目(默認路徑一般是:C:\Users\14065.m2)


4、maven編碼gbk不可映射字符

  • 是由於Eclipse等編輯器與本地編碼格式不一致所導致。

安裝系統之後,一般中文系統默認字符集是GBK。我們安裝的軟件一般都繼承使用操作系統的默認字符集。所以當在中文XP或者win7系統開發,在使用maven(mvn compile)編譯項目的時候,就會出現“編碼 gbk 的不可映射字符”: 這個是由於代碼使用的UTF-8,而maven編譯的時候使用的GBK的緣故。 可以通過修改項目的pom文件,可以告訴maven這個項目使用UTF-8編碼來編譯。在項目的pom.xml文件中添加下面的配置:

 <!-- 指明編譯源代碼時使用的字符編碼,maven編譯的時候默認使用的GBK編碼, 通過project.build.sourceEncoding屬性設置字符編碼,告訴maven這個項目使用UTF-8來編譯 -->  
<properties>  
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
</properties>


5、SpringMVC項目運行時找不到DispatcherServlet映射

  • 問題:

    No mapping found for HTTP request with URI [/courses/view3] in DispatcherServlet with name ‘mvc-dispatcher-servlet’

  • 原因一:查看對應的配置文件 mvc-dispatcher-servlet.xml 配置的路徑是否有誤

    <!-- 確保此路徑無誤 -->
    <context:component-scan base-package="com.xiao.mvcdemo">
      <context:include-filter type="annotation"
              expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
  • 原因二:查看 web.xml 的配置是否有誤

    <!-- DispatcherServlet, Spring MVC的核心 -->
    <servlet>
          <servlet-name>mvc-dispatcher</servlet-name>
          <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <!-- DispatcherServlet對應的上下文配置, 默認為/WEB-INF/$servlet-name$-servlet.xml
           -->
          <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
          <servlet-name>mvc-dispatcher</servlet-name>
          <!-- mvc-dispatcher攔截所有的請求-->
          <url-pattern>/</url-pattern>
    </servlet-mapping>

6、如何用maven下載源代碼

  • 路徑:window——Preferences——Maven中:勾選Download Artifact Sources

Maven下載源碼

7、eclipse項目上初現紅叉

  • 將所有的jar包重新導入,包括tomcat、jre。

  • 可能是eclipse默認使用的服務器不是你自己配置的,修改為自己配置的即可:右鍵項目——Properties——Targeted Runtimes 中選擇你自己的tomcat服務器(要仔細,比如系統默認的是Apache Tomcat 7,而你自己的是Tomcat 7 Server)

  • 更多類似問題參考


8、maven項目上有一個小紅叉

  • 報錯信息

Description Resource Path Location Type
Project configuration is not up-to-date with pom.xml. Run project configuration update rdc line 1 Maven Configuration Problem

  • 解決方案

右鍵項目,【Maven】–》【Update Project Configuration…】即可


解決方案

8、普通java項目出現小紅叉

  • 解決方案

右鍵項目——>properties——>project facets


解決方案

右鍵項目——>properties——>java Compiler


解決方案

9、修改maven的本地倉庫地址


配置為自己的maven地址

配置自己的本地倉庫

10、Eclipse智能提示沒用了

  • 解決方案

    • 1、window->Preferences->Java->Editor->content assist 把 Enable auto activation 選項打上勾 :


      並且在點後面加上.abcdefghijklmnopqrstuvwxyz
      此時重啟了eclipse還不成功,之後接着第二步驟

    • 2、window->Preferences->[Java] ->Editor->Content Assist->Advanced 把該勾選的斗勾選起來


自動提示

11、Eclipse中創建maven web項目后報錯

  • 問題描述

web.xml is missing and <failOnMissingWebXml> is set to true

  • 解決方案

在webapp目錄下創建”WEB-INF”,在”WEB-INF”下創建web.xml即可。


12、在啟動maven項目時,報錯Please ensure you are using JDK 1.4 or above and not a JRE

  • 報錯信息

Please ensure you are using JDK 1.4 or above and not a JRE

  • 報錯原因

eclipse默認是使用jre作為運行環境,而maven編譯需要jdk作為運行環境

  • 解決方案

window -> preferences -> Java -> installed jres,點擊add->選擇Standard VM,新加一個環境,設為默認環境,注意location選擇為你的jdk目錄。


解決方案

如果還是不行,就在你的項目上右擊,build path -> configure build path,編輯JRE System Library,選擇剛才新建的默認環境



13、tomcat啟動maven項目報錯:A child container failed during start

  • 報錯信息

嚴重: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]

嚴重: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost]]

  • 報錯原因

自己添加的 servlet.api包和tomcat的這個jar包衝突。

  • 解決方案
  • 刪掉servlet-api包

    <!– <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> –>

  • 在該依賴的配置中添加<scope></scope>節點

    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
    </dependency>


14、maven項目無法加載靜態的Xml,html,tsl

  • 報錯信息
嚴重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'bookServiceImpl': 
Unsatisfied dependency expressed through field 'bookDao'; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'bookDao' defined in file 
[C:\Tools\develop\developer\idea\project\SSMFrame\target\classes\com\ssm\frame\dao\BookDao.class]:
 Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory';
 nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sqlSessionFactory' defined in class path resource
 [spring/spring-dao.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.core.io.Resource[]' for property 'mapperLocations'; nested exception is java.lang.IllegalArgumentException: 
Could not resolve resource location pattern [classpath:com/ssm/frame/dao/mapper/**Mapper.xml]: 
class path resource [com/ssm/frame/dao/mapper/] cannot be resolved to URL because it does not exist

Caused by: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'bookDao' defined in file 
[C:\Tools\develop\developer\idea\project\SSMFrame\target\classes\com\ssm\frame\dao\BookDao.class]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring/spring-dao.xml]:Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.core.io.Resource[]' for property 'mapperLocations'; 
nested exception is java.lang.IllegalArgumentException: Could not resolve resource location pattern [classpath:com/ssm/frame/dao/mapper/**Mapper.xml]:
 class path resource [com/ssm/frame/dao/mapper/] cannot be resolved to URL because it does not exist

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring/spring-dao.xml]: Initialization of bean failed;nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.core.io.Resource[]' for property 'mapperLocations'; nested exception is java.lang.IllegalArgumentException: Could not resolve resource location pattern [classpath:com/ssm/frame/dao/mapper/**Mapper.xml]: 
class path resource [com/ssm/frame/dao/mapper/] cannot be resolved to URL because it does not exist

Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.core.io.Resource[]' for property 'mapperLocations'; 
nested exception is java.lang.IllegalArgumentException: Could not resolve resource location pattern [classpath:com/ssm/frame/dao/mapper/**Mapper.xml]: class path resource [com/ssm/frame/dao/mapper/] cannot be resolved to URL because it does not exist

Caused by: java.lang.IllegalArgumentException: Could not resolve resource location pattern [classpath:com/ssm/frame/dao/mapper/**Mapper.xml]: class path resource [com/ssm/frame/dao/mapper/] cannot be resolved to URL because it does not exist
  • 錯誤分析
  • 此時,你可以去項目的工作空間里看看,你會驚訝的發現並沒有生成 .xml等文件,或者生成的.class,文件並不是和你編寫的.xml、.java源文件的文件內容一致的,有可能你新加的一些方法或參數都不在其中,比如:我的BookEntityMapper.xml文件中的pojo引用為”com.ssm.frame.entity.BookEntity”,但是在生成的BookEntityMapper.class文件中是”com.ssm.frame.entity.Book”等錯誤。
  • 是因為你是用的是maven項目,maven項目都是依賴於jar配置,即依賴於pom.xml的配置,它不會自動的為你加載靜態.Xml,html,tsl 等文件,所以導致無法生成相應的.class文件。
  • 解決方案
<!--在pom.xml的build節點中加入以下配置-->
<resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.tld</include>
                    <include>**/*.jsp</include>
                </includes>
                <filtering>true</filtering>
            </resource>

            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.tld</include>
                </includes>
                <filtering>true</filtering>
            </resource>
</resources>


15、Spring加載時報錯: org.springframework.beans.factory.UnsatisfiedDependencyException

  • 報錯信息
org.springframework.beans.factory.UnsatisfiedDependencyException
  • 錯誤分析

Spring 的自動注入所依賴的jar包,缺少任何一個都無法成功通過Spring進行注入

  • 解決方案

<!–在pom.xml添加一下依賴,缺一不可–>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.12</version>
        </dependency>
        <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.5.3</version>
        </dependency>


16、springMVC項目啟動時JNDI NameNotFoundException

  • 錯誤信息
 [org.springframework.jndi.JndiLocatorDelegate] - Converted JNDI name [java:comp/env/spring.liveBeansView.mbeanDomain] not found - trying original name [spring.liveBeansView.mbeanDomain]. javax.naming.NameNotFoundException:
  • 解決方案
<!-- 在web.xml中添加 -->
<context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>dev</param-value>
    </context-param>
    <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>dev</param-value>
    </context-param>
    <context-param>
        <param-name>spring.liveBeansView.mbeanDomain</param-name>
        <param-value>dev</param-value>
    </context-param>


17、當數據庫中時間為“0000-00-00 00:00:00”時,加載到頁面時報錯

  • 解決方案
 jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false

<!-- zeroDateTimeBehavior=convertToNull -->