2012-04-06 41 views
5

我的问题是,即使设置“部署大会”,以包括Maven的依赖关系,这给了我的班级没有找到,我不知道还有什么要做。Maven + Spring +动态Web模块(Eclipse)= java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener

而我只是注意到ContextLoaderListener类,因为其他类似乎包含在我的包中。

我的文件的pom.xml

<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/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.mkyong.common</groupId> 
    <artifactId>SpringMVC</artifactId> 
    <packaging>war</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <name>SpringMVC Maven Webapp</name> 
    <url>http://maven.apache.org</url> 
    <dependencies> 
     <!-- <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> 
      <version>3.8.1</version> <scope>test</scope> </dependency> --> 

     <!-- Spring framework --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring</artifactId> 
      <version>2.5.6</version> 
     </dependency> 

     <!-- Spring MVC framework --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>2.5.6</version> 
     </dependency> 

     <!-- JSTL --> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.1.2</version> 
     </dependency> 

     <dependency> 
      <groupId>taglibs</groupId> 
      <artifactId>standard</artifactId> 
      <version>1.1.2</version> 
     </dependency> 

     <!-- for compile only, your container should have this --> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>2.5</version> 
      <scope>provided</scope> 
     </dependency> 

    </dependencies> 

    <build> 
     <finalName>SpringMVC</finalName> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>tomcat-maven-plugin</artifactId> 
       <version>1.0-beta-1</version> 
       <configuration></configuration> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

我的文件web.xml中

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Spring Web MVC Application</display-name> 

    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>*.htm</url-pattern> 
    </servlet-mapping> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> 
    </context-param> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

</web-app> 
+0

我编辑你的问题,因为你应该在你的问题用英语/答案/评论。 – 2012-04-06 17:43:04

+0

我不明白你的意思! – 2012-04-06 18:18:58

+1

第二段是用葡萄牙语写的,我把它翻译成英文,我们在这里只能用英文,所以大家可以理解 – 2012-04-06 20:56:29

回答

10

这个问题似乎要么是由于:

  1. 一些库版本不匹配(即。 java版本来编译!= java web服务器上的 版本)。
  2. 所需的Spring库在部署期间不包含。用于设置项目

  • Maven原型执行以下步骤来尝试解决这个问题:

    • 在pom.xml中>如果你使用Maven的编译器插件,仔细检查这是匹配使用的库的版本,如果不改变它(即1.7 NOT 1.6)。

      <plugin> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <configuration> 
          <source>1.7</source> 
          <target>1.7</target> 
      </configuration> 
      </plugin> 
      
    • 如果你改变了上述步骤库版本,那么你必须做:右击项目>的Maven>更新项目......如果在这一步抛出一个错误,你可能会发现自己陷入了困境为好。我有这个问题,无法解决它。我最终放弃了在Eclipse中使用原型创建“Maven的Spring MVC”。我相信使用原型搞砸了..在我的情况。

    • 右键单击项目>属性> Java构建路径> JRE系统库>编辑>选择正确的库版本

    • 右键单击项目>属性>项目构面>动态Web模块(应检查)>面板右侧的运行时选项卡>选择创建的服务器(以前必须添加服务器)。

    • 右键单击Project> Properties> Deployment Assembly> Add> Java Build Path Entries>选择Spring依赖关系。这是ClassNotFoundException while loading ContextLoaderListener注意:必须在eclipse

    • 记得清理你的项目做一个“的Maven>更新项目”后,每次执行该步骤之前将其部署:项目>清洁 ...>选择项目>清洁

    • 您的项目部署,看看它不破...希望

    就我而言,我不得不使用Maven原型来获得准备重新启动一个新项目没有。我使用这些指导http://gkokkinos.wordpress.com/2012/01/02/setting-up-a-spring-mvc-project-with-maven-in-eclipse/。我仍然遇到了一个错误,但通过部署汇编(如上所述)添加Spring依赖关系可以解决问题。

  • 2

    我有这个相同的问题。简单的解决方案是右键单击顶层项目 - >属性 - >部署程序集,并包含'Maven Dependencies'。在我的情况下,这是我的问题所在。 DispatcherServlet类没有包含在war文件中,因此当我将它部署到该服务器时,Tomcat没有找到它。

    0

    我有类似的问题,并按照这些步骤来解决它。

    1. 在eclipse中打开服务器选项卡。右键点击你的服务器。
    2. 清洁(Tomcat)的工作目录
    3. 清洁(这是比工作目录不同的清理,你可以在右键单击上下文看)
    相关问题