【问题标题】:Maven + Spring + Dynamic Web Module ( Eclipse ) = java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListenerMaven + Spring + 动态 Web 模块 (Eclipse) = java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
【发布时间】:2012-04-20 06:12:23
【问题描述】:

我的问题是,即使将“部署程序集”设置为包含 maven 依赖项,这会导致找不到我的类,我不知道还能做什么。

我只是注意到 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>

【问题讨论】:

  • 我编辑了您的问题,因为您应该在问题/答案/cmets 中只使用英语。
  • 我不明白你的意思!
  • 第二段是葡萄牙语,我翻译成英文,这里只能用英文,大家都能看懂
  • 请添加您创建项目所遵循的步骤。谢谢

标签: eclipse spring maven


【解决方案1】:

这个问题似乎是由于:

  1. 某些库版本不匹配(即要编译的 java 版本!= java Web 应用服务器上的版本)。
  2. 部署期间未包含必需的 Spring 库。
  3. 用于设置项目的 Maven 原型

执行以下步骤以尝试解决此问题:

  • 在 pom.xml > 如果您使用 maven-compiler-plugin,请仔细检查这是否与使用的库的版本匹配,如果不更改它(即 1.7 而不是 1.6)。

    <plugin>
     <artifactId>maven-compiler-plugin</artifactId>
     <configuration>
      <source>1.7</source>
      <target>1.7</target>
     </configuration>
    </plugin>
    
  • 如果您在上述步骤中更改了库版本,则必须执行以下操作:右键单击 Project > Maven > Update Project... 如果在此步骤中抛出错误,您可能会发现自己永远卡住了.我遇到了这个问题,无法解决。我最终放弃了使用原型在 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/。我仍然抛出了一个错误,但是通过 Deployment Assembly(如上所述)添加 Spring 依赖项可以解决问题。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。简单的解决方案是右键单击顶级项目-> 属性-> 部署程序集并包含“Maven 依赖项”。就我而言,这就是我的问题所在。类 DispatcherServlet 没有包含在 war 文件中,因此当我将它部署到该服务器时,Tomcat 没有找到它。

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题,并按照以下步骤进行了修复。

      1. 在 Eclipse 中打开服务器选项卡。右键单击您的服务器。
      2. 清理 (Tomcat) 工作目录
      3. 清理(这与您可以在右键单击上下文中看到的工作目录清理不同)

      【讨论】:

        猜你喜欢
        • 2013-12-27
        • 2016-07-03
        • 2014-02-24
        • 2012-07-20
        • 1970-01-01
        • 1970-01-01
        • 2014-01-18
        • 2014-02-18
        • 1970-01-01
        相关资源
        最近更新 更多