为spring配置web.xml和spring dispatcher-servlet.xml

时间:2014-09-24 22:29:46

标签: xml spring spring-mvc

我是Spring框架的新手,所以当我尝试创建一个简单的spring MVC程序时,我的服务器会显示这个异常报告(精确地找不到类异常):

如果图片不正确,请检查下面的完整错误列表: enter image description here

我已经添加了spring分发jar,commons-logins jar和servlet等。

我的XML文件如下:

的web.xml

<web-app id="WebApp_ID" version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <display-name>FirstSpringMVCProject</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>

弹簧调度-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-2.5.xsd">  



    <bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <bean name="/welcome.html" class="com.gontuseries.hellocontroller.HelloController"/>

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

        <property name="prefix" value="/WEB-INF" />  
        <property name="suffix" value=".jsp" />  
    </bean>  

</beans>  

有关详细信息,请参阅完整的错误列表:

INFO: Starting ProtocolHandler ["http-nio-9999"]
Sep 25, 2014 3:11:11 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Sep 25, 2014 3:11:11 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3968 ms
Sep 25, 2014 3:11:12 AM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet spring-dispatcher as unavailable
Sep 25, 2014 3:11:12 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet spring-dispatcher
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1320)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1173)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:550)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:531)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:150)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1105)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:534)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

4 个答案:

答案 0 :(得分:2)

在使用Eclipse的Spring MVC时遇到了同样的问题。会发生什么是将依赖项添加到Eclipse的类路径中。然而,下一步是错过了

  

1)使用:properties&gt;将依赖项添加到类路径中。   buildpath - eclipse中项目的选项

如果您已经添加了依赖项,那么您希望为Web应用程序执行下一步

  

2)转到部署程序集(项目&gt;属性&gt;部署   程序集)点击添加:选择“Java Build Path Entries”选择   依赖项添加。单击“完成”。

理想情况下,第2步应解决您的问题。

答案 1 :(得分:0)

这是普通的Spring MVC Web应用程序所需的依赖项列表。

enter image description here

您可以在

下载/克隆/查看项目

https://github.com/desorder/spring-mvc-test

这是一个日食项目。

答案 2 :(得分:0)

春天就像一个国家:

dispatcher-servlet.xml是Spring镇邮件的邮局,每封信都应该通过它们,但有些人继续用旧的web.xml县办公室发送东西(应该只是一个中介),这是曾经伟大而独立的国家的一部分,但现在只是一个地区。可能是旧的而不是花哨的,但是在适当管理时效果很好,web.xml县的某些区域的道路通向404米深的悬崖。

答案 3 :(得分:0)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  http://www.springframework.org/schema/cache 
        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <mvc:annotation-driven />
    <mvc:default-servlet-handler />
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <context:component-scan base-package="com.cango.optigo"/>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> 
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <!-- Enables the caching through annotations -->
    <cache:annotation-driven />

    <!-- Configuration for using Ehcache as the cache manager-->
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
          <set>
            <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="testcache" />
          </set>
        </property>
    </bean>


    <!-- declare datasource bean -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/testdb" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

</beans>