如何为spring配置web.xml

时间:2012-11-25 22:09:17

标签: java spring spring-mvc web.xml classnotfoundexception

我是Spring Framework的新手,并且在通过web.xml链接/配置springframework时遇到了问题。 运行我的应用程序时出现以下错误。

错误:

HTTP Status 500 - Error instantiating servlet class org.springframework.web.servlet.DispatcherServlet


    type Exception report

    message Error instantiating servlet class org.springframework.web.servlet.DispatcherServlet

    description The server encountered an internal error (Error instantiating servlet class org.springframework.web.servlet.DispatcherServlet) that prevented it from fulfilling this request.

    exception 

    javax.servlet.ServletException: Error instantiating servlet class org.springframework.web.servlet.DispatcherServlet
        org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
        org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
        org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
        org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
        java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        java.lang.Thread.run(Unknown Source)


    root cause 

    java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
        org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711)
        org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
        org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
        org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
        org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
        org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
        org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
        java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        java.lang.Thread.run(Unknown Source)

显然,它无法在库中找到类org.springframework.web.servlet.DispatcherServlet。但是,我不知道如何告诉web.xml在哪里寻找这个类。

的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" 
             xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
             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">

        <servlet>
            <servlet-name>DispatcherSample</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        </servlet>

        <servlet-mapping>
            <servlet-name>DispatcherSample</servlet-name>
            <url-pattern>/PlayField.html</url-pattern>
        </servlet-mapping>

      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>Welcome.html</welcome-file>
      </welcome-file-list>

    </web-app>

DispatcherSample-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"
        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.xsd">

        <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
            <property name="mappings">
                <props>
                    <prop key="/PlayField.html">playfieldController</prop>
                </props>    
            </property>
        </bean>

        <bean id="playfieldController" class="demos.PlayfieldController"/>

    </beans>

1 个答案:

答案 0 :(得分:3)

  

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

您必须将spring-webmvc.jar添加到CLASSPATH(最有可能是/WEB-INF/lib目录)。其余配置似乎没问题。