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
问题描述
我只是注意到使用 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>
- 某些库版本不匹配(即在java应用程序服务器上编译java代码= java
)。 - 在部署期间不包含必需的Spring库。
- 用于设置项目的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 My file pom.xml My file web.xml
This problem seems to be either due to: Perform these steps to try fix this issue: In pom.xml > if you use maven-compiler-plugin, double check this is matching the version of the library used, if not change it (ie. 1.7 NOT 1.6). If you changed the library version in the above step, you then must do: Right Click on Project > Maven > Update Project... If an error is thrown during this step, you may find yourself stuck for good. I had this issue and could not resolve it. I ended up giving up creating my "Spring MVC with Maven" in Eclipse using archetypes. I believe using archetypes messed things up..in my case. Right Click on Project > Properties > Java Build Path > JRE System Library > Edit > choose correct library version Right Click on Project > Properties > Project Facets > Dynamic Web module (should be checked) > Runtimes tab on the right side of the panel > choose the server created (you must have added a server previously). Right Click on Project > Properties > Deployment Assembly > Add > Java Build Path Entries > select the Spring dependencies. This is from ClassNotFoundException while loading ContextLoaderListener Note: You must perform this step every time after doing a "Maven > Update Project" in eclipse Remember to clean your project before to deploy it: Project > Clean
... > select project > clean Deploy your project and see it NOT break... hopefully 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屋!ContextLoaderListener
class since other classes seem to be included in my package.<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-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>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>