spring java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11227395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:12:25  来源:igfitidea点击:

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

springmavenspring-mvcmaven-2

提问by Mahmoud Saleh

i am using spring 3.1.0.RELEASE, and my servlet container is tomcat 7and my IDE is eclipse indigoand the jar spring-webmvc-3.1.0.RELEASE.jarwhich contains the DispatcherServletexists in the lib folder, and yet when running the application, i am getting the exception:

我正在使用spring 3.1.0.RELEASE,我的 servlet 容器是tomcat 7,我的 IDE 是eclipse indigo并且包含DispatcherServlet的 jar spring-webmvc-3.1.0.RELEASE.jar存在于 lib 文件夹中,但是当运行应用程序,我收到异常:

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:525)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:507)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:126)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1043)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4957)
    at org.apache.catalina.core.StandardContext.call(StandardContext.java:5284)
    at org.apache.catalina.core.StandardContext.call(StandardContext.java:5279)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

please advise why i am getting this exception, and how to fix it.

请告知为什么我会收到此异常,以及如何修复它。

EDIT: following are my configuration files:

编辑:以下是我的配置文件:

1- .springBeans:

1- .springBeans:

<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
    <version>1</version>
    <pluginVersion><![CDATA[2.9.0.201203011806-RELEASE]]></pluginVersion>
    <configSuffixes>
        <configSuffix><![CDATA[xml]]></configSuffix>
    </configSuffixes>
    <enableImports><![CDATA[false]]></enableImports>
    <configs>
        <config>src/main/webapp/WEB-INF/checkout-servlet.xml</config>
    </configs>
    <configSets>
    </configSets>
</beansProjectDescription>

2- web.xml:

2- web.xml:

<web-app>
  <display-name>Checkout</display-name>

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

  <servlet-mapping>
    <servlet-name>checkout</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>

</web-app>

3- checkout-servlet.xml:

3-结帐-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:component-scan base-package="com.myapp"/>

    <bean id="myService" class="com.myapp.MyService"/>

</beans>

also when trying to access any page in the application, i get the exception:

同样在尝试访问应用程序中的任何页面时,我也收到异常:

HTTP Status 404 - Servlet checkout is not available

type Status report

message Servlet checkout is not available

description The requested resource (Servlet checkout is not available) is not available.
Apache Tomcat/7.0.22

采纳答案by Mahmoud Saleh

i found that in the deployment assembly, there was the entry:

我发现在部署程序集中,有条目:

[persisted container] org.maven.ide.eclipse.maven2_classpath_container

i removed it, and added the maven dependenciesentry, and it works fine now.

我删除了它,并添加了maven dependencies条目,现在它工作正常。

回答by krakos

You need to add the "Maven Dependency" in the Deployment Assembly

您需要在部署程序集中添加“Maven 依赖项”

  • right click on your project and choose properties.
  • click on Deployment Assembly.
  • click add
  • click on "Java Build Path Entries"
  • select Maven Dependencies"
  • click Finish.
  • 右键单击您的项目并选择属性。
  • 单击部署程序集。
  • 点击添加
  • 单击“Java 构建路径条目”
  • 选择 Maven 依赖项”
  • 单击完成。

Rebuild and deploy again

重新构建和部署

Note: This is also applicable for non mavenproject.

注意:这也适用于非 maven项目。

回答by Serkan Ar?ku?u

Two possible answers:

两种可能的答案:

1- You did not include spring-beans and spring-context jars in your lib. If you are using maven (which will help a lot) those two lines will be enough

1- 您的库中没有包含 spring-beans 和 spring-context jar。如果您使用的是 maven(这将有很大帮助),这两行就足够了

<dependency>
 <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
   <version>3.1.0.RELEASE</version>
</dependency>
<dependency>
 <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
   <version>3.1.0.RELEASE</version>
</dependency>

2- The necessary jars are in your classpath but are not deployed on tomcat.

2- 必要的 jars 在您的类路径中,但未部署在 tomcat 上。

回答by Adelin

Hi guys I had the same problem with Idea Intellij and Spring 4 I fixed the problem and I wanted to share the answer with you.

大家好,我在使用 Idea Intellij 和 Spring 4 时遇到了同样的问题,我解决了这个问题,我想与您分享答案。

I use tomcat 7 / idea intellij 13 / spring 4

我使用 tomcat 7/idea intellij 13/spring 4

pom.xml dependencies

pom.xml 依赖

  <properties>
    <spring.version>4.0.5.RELEASE</spring.version>
  </properties>

  <dependencies>     
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
enter code here
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.2</version>
    </dependency>
        <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
  </dependencies>

In idea Intellij you need to go to File -> Project Settings -> Artifacts. Then enter image description here

在idea Intellij 中,您需要转到文件-> 项目设置-> 工件。然后 在此处输入图片说明

clean -> rebuild the project -> build the artifact and everything will be ok ...

清理 -> 重建项目 -> 构建工件,一切都会好起来的......

回答by Lycone

This solves the problem for me. It's easy and pretty simply explained.

这为我解决了问题。这很容易,而且解释得很简单。

Step 1

第1步

  • Right click on project
  • Click on Properties
  • 右键单击项目
  • 点击属性

Step 2

第2步

  • Click on Deployment Assembly Tab in the
  • Click Add...
  • 单击“部署程序集”选项卡中的
  • 点击添加...

Step 3

第 3 步

  • Click on Java Build Path Entries
  • 单击 Java 构建路径条目

Step 4

第四步

  • Click on Maven Dependencies
  • Click Finish button
  • 单击 Maven 依赖项
  • 单击完成按钮

Step 5

第 5 步

  • Redeploy Spring MVC application to Tomcat again
  • Restart Tomcat
  • List item
  • 再次将 Spring MVC 应用程序重新部署到 Tomcat
  • 重启Tomcat
  • 项目清单

classnotfoundexception

类未找到异常

回答by Chinbold Gansukh

If all of these advice doesn't work, you should re-create your Server (Tomcat or like that). That solved my problem.

如果所有这些建议都不起作用,您应该重新创建您的服务器(Tomcat 或类似的)。那解决了我的问题。

回答by Victor

Include below dependency in your pom.xml

在 pom.xml 中包含以下依赖项

<dependency>
 <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
   <version>{spring-version}</version>
</dependency>

回答by TKS

I was facing the same Issue. When I saw into maven repository .m2 folder(....m2\repository\org\springframework\spring-webmvc) in my local I found two 3.2.0.RELEASE folders. SO I removed one. Then I went to project, right click->properties->deployment essembly-> add maven dependencies. clean build and then restart the server. Then the DispatcherServlet got loaded.

我面临同样的问题。当我在本地看到 maven repository .m2 folder(....m2\repository\org\springframework\spring-webmvc) 时,我发现了两个 3.2.0.RELEASE 文件夹。所以我删除了一个。然后我去project,右键->properties->deployment essemly->添加maven依赖。清理构建,然后重新启动服务器。然后 DispatcherServlet 被加载。

回答by user2947452

In my case I get this trouble after using the maven's update project utility. I tried all the workarounds you suggested but nothing seemed to work. At the end the solution was simply to remove the project from the server to ensure that it was clean, and add it again. Then it works, I hope this solution could help any of you.

在我的情况下,我在使用 Maven 的更新项目实用程序后遇到了这个麻烦。我尝试了您建议的所有解决方法,但似乎没有任何效果。最后的解决方案只是从服务器中删除项目以确保它是干净的,然后再次添加它。然后它起作用了,我希望这个解决方案可以帮助你们中的任何人。

回答by Superb Saif

You can use GlassFish server and the error will be resolved. I tried with tomcat7 and tomcat8 but this error was coming continuously but resolved with GlassFish. I think it's a problem with server.

您可以使用 GlassFish 服务器,错误将得到解决。我尝试使用 tomcat7 和 tomcat8,但此错误不断出现,但已通过 GlassFish 解决。我认为是服务器的问题。

These are the results with tomcat7: Results with tomcat7

这些是使用 tomcat7 的结果: tomcat7 的结果

Here are the results with GlassFish: Results with GlassFish

以下是使用 GlassFish 的结果: GlassFish 的结果