无法加载org.springframework.web.servlet.DispatcherServlet

时间:2016-04-10 16:20:57

标签: java spring spring-mvc tomcat

我正在尝试设置spring MVC,但是当我启动tomcat时,我得到了这个异常,我正在使用带有Hibernate的spring。 Hibernate工作正常,但Spring MVC给出了异常。enter image description here

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:520)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:501)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:120)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1095)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4997)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5289)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745

我正在使用的罐子

  1. commons-logging-1.2
  2. spring-4.2.4.RELEASE.zip,其中包括所有春天的罐子
  3. 的log4j
  4. 我的web.xml代码

    `<?xml version="1.0" encoding="ISO-8859-1"?>
    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                          http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
      version="3.1">
    
    
    
        <servlet>
            <servlet-name>default</servlet-name>
            <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
    
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
         <servlet>
            <servlet-name>mvc</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>mvc</servlet-name>
            <url-pattern>/HTML/</url-pattern>
            <url-pattern>/jsp/</url-pattern>
    
        </servlet-mapping>
          <servlet-mapping>
            <servlet-name>jsp</servlet-name>
            <url-pattern>*.jsp</url-pattern>
            <url-pattern>*.jspx</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    `我的mvc-servlet.xml代码

    `<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="com.pos.controller" />
    
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass"
                value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
    </beans>`
    

    我使用ctrl + shift + t在类路径中搜索它 org.springframework.web.servlet.DispatcherServlet 存在 我的测试MVC java代码`@Controller

    public class HelloController{
        @RequestMapping("/welcome")
        public ModelAndView helloWorld() {
    
            String message = "<br><div style='text-align:center;'>"
                    + "<h3>********** Hello World, Spring MVC Tutorial</h3> **********</div><br><br>";
            return new ModelAndView("welcome", "message", message);
        }
       }`
    

    请不要将其标记为重复我之前已经看过所有重复的问题,并且还尝试了他们要求的工作人员,但仍有同样的异常告诉我我做错了什么

2 个答案:

答案 0 :(得分:0)

您错过了web.xml添加此内容的上下文加载程序侦听器:

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

如果您从eclipse托管的tomcat运行应用程序,请确保您的部署程序集包含所有jar。请查看此answer了解具体详情。

答案 1 :(得分:0)

我已经解决了问题我做错了是因为我使用tomcat的web.xml这么愚蠢,因为我在创建项目时没有生成web.xml。