Maven + Spring +动态Web模块(Eclipse)= java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener [英] Maven + Spring + Dynamic Web Module ( Eclipse ) = java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

查看:109
本文介绍了Maven + Spring +动态Web模块(Eclipse)= java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,甚至设置部署程序集以包含maven依赖项,这样我的类没有被找到,我也不知道还有什么要做。



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



我的文件pom.xml

 < project xmlns =http://maven.apache.org/POM /4.0.0xmlns: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>
<依赖关系>
<! - < dependency> <&的groupId GT; junit的< /&的groupId GT; < artifactId的> junit的< / artifactId的>
< version> 3.8.1< / version> <范围>试验< /范围> < /依赖性> - >

<! - Spring框架 - >
<依赖关系>
< groupId> org.springframework< / groupId>
< artifactId> spring< / artifactId>
< version> 2.5.6< / version>
< / dependency>

<! - Spring MVC框架 - >
<依赖关系>
< groupId> org.springframework< / groupId>
< artifactId> spring-webmvc< / artifactId>
< version> 2.5.6< / version>
< / dependency>

<! - JSTL - >
<依赖关系>
< groupId> javax.servlet< / groupId>
< artifactId> jstl< / artifactId>
< version> 1.1.2< / version>
< / dependency>

<依赖关系>
< groupId> taglibs< / groupId>
< artifactId> standard< / artifactId>
< version> 1.1.2< / version>
< / dependency>

<! - 仅供编译,您的容器应该具有 - >
<依赖关系>
< groupId> javax.servlet< / groupId>
< artifactId> servlet-api< / artifactId>
< version> 2.5< / version>
< scope>已提供< / 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_IDversion =2.4
xmlns =http://java.sun.com/xml/ns/j2eexmlns: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应用程序< / 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>


解决方案

这个问题似乎是由于: p>


  1. 某些库版本不匹配(即在java应用程序服务器上编译java代码= java
    )。

  2. 在部署期间不包含必需的Spring库。

  3. 用于设置项目的Maven原型

执行以下步骤尝试修复此问题:




  • 在pom.xml中>如果您使用maven-compiler - 插件,双重检查这是使用的库的版本,如果不更改它(即1.7不是1.6)。

     <插件> 
    < artifactId> maven-compiler-plugin< / artifactId>
    < configuration>
    < source> 1.7< / source>
    < target> 1.7< / target>
    < / configuration>
    < / plugin>


  • 如果您在上述步骤中更改了库版本,则必须执行以下操作:右键单击在项目> Maven>更新项目...如果在此步骤中抛出错误,您可能会发现自己陷入困境。我有这个问题,无法解决。我最终放弃在Eclipse中使用原型创建我的Spring MVC with Maven。我相信在我的情况下使用原型搞乱了事情。


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


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


  • 右键单击项目>属性>部署程序集>添加> Java构建路径条目>选择Spring依赖。这是从加载ContextLoaderListener 时的ClassNotFoundException注意:您必须在每次执行 eclipse中的更新项目


  • 记住要清理项目以进行部署:项目>清理
    ...>选择项目>清理


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




在我的情况下,我不得不使用maven原型重新启动一个没有的新项目来实现。我使用了这些指南

My problem is that even set the "Deployment Assembly" to include the maven dependencies, this giving that my class is not being found, I do not know what else to do.

And I'm just noticing that with the ContextLoaderListener class since other classes seem to be included in my package.

My file 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>

My file 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>

解决方案

This problem seems to be either due to:

  1. Some library version mismatch (ie. java version to compile != java version on web app server).
  2. Required Spring library not included during deployment.
  3. Maven archetypes used to setup the project

Perform these steps to try fix this issue:

In my case, I had to restart a new project without using maven archetypes to get going. I used these guidelines http://gkokkinos.wordpress.com/2012/01/02/setting-up-a-spring-mvc-project-with-maven-in-eclipse/ . I still had an error thrown, but adding the Spring dependencies via Deployment Assembly (as described above) fixed things up.

这篇关于Maven + Spring +动态Web模块(Eclipse)= java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆