【问题标题】:java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in spring projectjava.lang.ClassNotFoundException:春季项目中的 org.springframework.web.servlet.DispatcherServlet
【发布时间】:2013-06-27 04:34:00
【问题描述】:

一个hello.jsp

web.xml 是

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 版本="3.0">

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- The front controller of this Spring Web application, responsible for 
    handling all application requests -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>

</servlet>

<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

dispatcher-servlet.xml 是

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"

xmlns:p="http://www.springframework.org/schema/p">

<bean id="viewResolver"

    class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix">

        <value>/WEB-INF/jsp/</value>

    </property>

    <property name="suffix">

        <value>.jsp</value>

    </property>

</bean>
<bean name="/hello.html" class="com.spring.HelloWorldController"></bean>
</beans>

JAR 文件是: spring.jar spring-webmvc.jar spring-aop 春豆 弹簧上下文 弹簧上下文支持 弹簧芯 spring-jdbc 弹簧 泉源 弹簧测试 spring-tx

【问题讨论】:

  • 你添加了spring-framework库和spring mvc库吗?
  • @TI:很明显异常是因为org.springframework.web不存在引起的
  • @Rahul 是的,所有库都已添加
  • @SagarVaghela - 部署的 WEB-INF/lib 目录中有哪些 Spring JAR?请在问题中列出它们。
  • @StephenC 我已经展示了所有有问题的弹簧罐

标签: java


【解决方案1】:

在我的例子中,我使用了 Ivy,但我遇到了同样的问题。你可以做这两个中的任何一个

  1. 将您的库移动到 WEB-INF/lib 。因为这是 Eclipse 从中搜索相应 jar 的文件夹。或
  2. 让 Eclipse 知道它可以从 ivy 库文件夹中搜索 jars 这与 WEB-INF/lib 不同,即通过项目属性更改部署程序集中的 java 构建路径。

对于第二种方法,您可以参考details post with screenshots(链接到我的个人博客,了解更多详情)。或者你也可以去看看我问过here的类似问题。

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,我通过这种方式解决了。如果添加了所有必需的库并且您仍然收到此错误。尝试在命令行中运行它:

    mvn eclipse:eclipse
    

    然后

    mvn clean install
    

    如果这不能解决问题,请右键单击您的 Eclipse 项目, 转到 >> 属性 >> 目标运行时 然后点击

    旁边的复选框

    apache tomcat v8.0

    取决于你的tomcat版本。 如果您正在运行 jboss,请选择 jboss 版本。 然后在此之后再次运行上述 2 个命令(mvn eclipse:eclipse 和 mvn clean install)。

    【讨论】:

      【解决方案3】:

      问题:java.lang.ClassNotFoundException:spring 项目中的 org.springframework.web.servlet.DispatcherServlet。

      通过将以下 jars 添加到 WEB-INF/Lib 文件夹中,我们可以解决此问题。

      org.springframework.asm-3.1.4.RELEASE.jar
      org.springframework.aspects-3.1.4.RELEASE.jar
      org.springframework.beans-3.1.4.RELEASE.jar
      org.springframework.context-3.1.4.RELEASE.jar
      org.springframework.context.support-3.1.4.RELEASE.jar
      org.springframework.core-3.1.4.RELEASE.jar
      org.springframework.web.struts-3.1.4.RELEASE.jar
      org.springframework.web.servlet-3.1.4.RELEASE.jar
      org.springframework.web-3.1.4.RELEASE.jar
      

      当然,您将被添加到构建路径中,但它只需要编译时间。所以我们必须将上面的 jars 添加到 WEB-INF/Lib 文件夹中

      【讨论】:

        【解决方案4】:

        问题: java.lang.ClassNotFoundException:spring 项目中的 org.springframework.web.servlet.DispatcherServlet

        问题是正确的类路径中没有必要的 jar

        解决方案

        将所有必要的 jars 放在类路径中。由于项目是动态 webproject,所以将所有 spring jars 放在 WEB-INF/Lib 文件夹中

        问题将得到解决

        【讨论】:

        • 所有必要的 jar 文件都已经添加到项目 WEB-INF/Lib 文件夹中,并且也添加到了类路径中..
        【解决方案5】:

        ClassNotFoundException 清楚地表明您缺少org.springframework.web.servlet 类。

        如果您不使用 Maven,请确保包含所有适当的 Spring JAR。

        如果您使用的是 Maven,请确保包含 spring-web 依赖项:

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version><!-- Your spring version here --></version>
            </dependency>
        

        如果这些都不起作用,take a look at this thread

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-11-22
          • 2011-10-07
          • 1970-01-01
          • 2023-03-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多