简体   繁体   中英

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

one hello.jsp

web.xml is

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

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="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 is

<?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 Files are: spring.jar spring-webmvc.jar spring-aop spring-beans spring-context spring-context-support spring-core spring-jdbc spring-orm spring-source spring-test spring-tx

The ClassNotFoundException clearly indicates that you are missing org.springframework.web.servlet classes.

If you are not using Maven , make sure you include all the appropriate Spring JARs.

If you are using Maven , make sure you include the spring-web dependency:

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

If none of these work, take a look at this thread .

In my case I used Ivy and I faced the same issue. You can do either of the two

  1. Either move your libraries to WEB-INF/lib . Because this is the folder from where Eclipse searches for corresponding jars. OR
  2. Let Eclipse know that it can search the jars from ivy library folder which is not same as WEB-INF/lib ie Change java build path in Deployment Assembly via Project properties.

For 2nd approach you can refer details post with screenshots (Link to my personal blog with more detials). Or you can also go through similar question I asked here .

Problem: java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in spring project

The issue is necessary jar are not present in the proper classpath

Solution

Place all the necessary jars in the classpath .Since the project is dynamic webproject place all the spring jars in the WEB-INF/Lib folder

The issue will be resloved

I had a similar issue and I resolved it this way. If all the required libraries are added and you are still getting this error. Try running this in command line:

mvn eclipse:eclipse

then

mvn clean install

If this doesn't resolve it, rightclick on your eclipse project, go to >>properties >> targeted runtime then click the checkbox next to

apache tomcat v8.0

depending on the version of you tomcat. If you are running jboss, choose a jboss version instead. and then after this run the above 2 commands(mvn eclipse:eclipse and mvn clean install) again.

Problem: java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in spring project.

By adding the below jars into WEB-INF/Lib folder we can resolve this issue.

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

Of Course you will be added in Build Path but it will take up to Compile time only. So we have to added above jars into WEB-INF/Lib folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM