实例化servlet类org.springframework.web.servlet.Dispatcherservlet时出错

时间:2016-06-19 05:27:49

标签: java spring spring-mvc

我正在使用spring 4.0.4 RELEASE.jar,但我找不到类异常。 这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns="http://java.sun.com/xml/ns/javaee"  
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"
version="3.0">
<display-name>SpringMvcApplication</display-name>
<servlet>
   <servlet-name>spring-dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.Dispatcherservlet</servlet-class>
 </servlet>
   <servlet-mapping>
 <servlet-name>spring-dispatcher</servlet-name>
   <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

这是我的spring-dispatcher-servlet.xml代码

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 <context:component-scan base-package="com.javahash.spring.controller" />
       <bean id="HandlerMapping" name="/welcome.html"
  class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"       />
   <bean name="/welcome.html"   
      class="com.manikant.hellocontroller.HelloController"/>    
   <bean id= "viewResolver"  

 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <property name="prefix" value="/WEB-INF/" />
       <property name="suffix" value=".jsp" />
    </bean>
 </beans>

这是堆栈跟踪:

javax.servlet.ServletException: Error instantiating servlet class org.springframework.web.servlet.Dispatcherservlet
            org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
            org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
            org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
            org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
            org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
            org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
            org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
            org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
            java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
            java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
            org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
            java.lang.Thread.run(Thread.java:745)

                java.lang.ClassNotFoundException:        
        org.springframework.web.servlet.Dispatcherservlet
            org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308)
            org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1142)
            org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
            org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
            org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
            org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
            org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
            org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
            org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
            org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
            java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
            java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
            org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
            java.lang.Thread.run(Thread.java:745)

2 个答案:

答案 0 :(得分:1)

请注意错误消息:

Error instantiating servlet class org.springframework.web.servlet.Dispatcherservlet
                                                                            ^

以及稍后的堆栈跟踪:

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

正确拼写为DispatcherServlet,大写为S

答案 1 :(得分:0)

长期解决方案

使用Spring 4.X及更高版本时,始终建议使用它们提供的Bill of Materials。它提取相关的依赖项,并帮助避免应用程序导致的所有类型的构建路径/运行时错误。

在您的情况下,请将这些依赖项添加到您的POM.xml文件或他们提供的上述材料清单中(选择是您的)

                             <dependencies>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-aop</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-aspects</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-beans</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-context</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-context-support</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-core</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-expression</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-instrument</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-instrument-tomcat</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-jdbc</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-jms</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-messaging</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-orm</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-oxm</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-test</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-tx</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-web</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-webmvc</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-webmvc-portlet</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>
                                  <dependency>
                                    <groupId>org.springframework</groupId>
                                    <artifactId>spring-websocket</artifactId>
                                    <version>4.0.4.RELEASE</version>
                                  </dependency>